2016-11-14 63 views
1

我正在使用搜索栏使用亮度代码。它需要很长时间才能在imageview中显示亮度。增加或减少图像亮度使用seekbar的作品非常慢

下面我发布的代码:

BrightActivity.java:

import android.graphics.ColorMatrix; 
import android.graphics.ColorMatrixColorFilter; 
import android.os.Bundle; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Color; 

import android.widget.ImageView; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 

public class BrightActivity extends Activity { 


    SeekBar seekbarbrightness; 

    Bitmap largeIcon; 

    ImageView ivDisImage; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.bright_activity); 



     seekbarbrightness = (SeekBar) findViewById(R.id.seekBar1); 
     ivDisImage = (ImageView)findViewById(R.id.imageView1); 

     largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.lhota); 



     seekbarbrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 


      @Override 
      public void onStopTrackingTouch(SeekBar arg0) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onStartTrackingTouch(SeekBar arg0) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) { 

       // TODO Auto-generated method stub 

       Bitmap bitmap=doBrightness(largeIcon, progress); 
       ivDisImage.setImageBitmap(bitmap); 
       ivDisImage.setColorFilter(brightIt(100)); 

      } 
     }); 


    } 


    public static Bitmap doBrightness(Bitmap src, int value) { 
     // image size 
     int width = src.getWidth(); 
     int height = src.getHeight(); 
     // create output bitmap 
     Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig()); 
     // color information 
     int A, R, G, B; 
     int pixel; 

     // scan through all pixels 
     for(int x = 0; x < width; ++x) { 
      for(int y = 0; y < height; ++y) { 
       // get pixel color 
       pixel = src.getPixel(x, y); 
       A = Color.alpha(pixel); 
       R = Color.red(pixel); 
       G = Color.green(pixel); 
       B = Color.blue(pixel); 

       // increase/decrease each channel 
       R += value; 
       if(R > 255) { R = 255; } 
       else if(R < 0) { R = 0; } 

       G += value; 
       if(G > 255) { G = 255; } 
       else if(G < 0) { G = 0; } 

       B += value; 
       if(B > 255) { B = 255; } 
       else if(B < 0) { B = 0; } 

       // apply new pixel color to output bitmap 
       bmOut.setPixel(x, y, Color.argb(A, R, G, B)); 
      } 
     } 

     // return final image 
     return bmOut; 
    } 

    public static ColorMatrixColorFilter brightIt(int fb) { 
     ColorMatrix cmB = new ColorMatrix(); 
     cmB.set(new float[] { 
       1, 0, 0, 0, fb, 
       0, 1, 0, 0, fb, 
       0, 0, 1, 0, fb, 
       0, 0, 0, 1, 0 }); 

     ColorMatrix colorMatrix = new ColorMatrix(); 
     colorMatrix.set(cmB); 
//Canvas c = new Canvas(b2); 
//Paint paint = new Paint(); 
     ColorMatrixColorFilter f = new ColorMatrixColorFilter(colorMatrix); 
//paint.setColorFilter(f); 
     return f; 
    } 



} 

bright_activity.java:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:text="Brightness" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <SeekBar 
     android:id="@+id/seekBar1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView2" 
     android:layout_marginTop="20dp" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="200dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/seekBar1" 
     android:layout_marginTop="18dp" 
     android:src="@drawable/lhota" /> 

</RelativeLayout> 

任何人都知道如何增加和减少的亮度使用seekbar更快的图像。

+0

http://stackoverflow.com/questions/18312609/change-the-system -brightness-programmatically –

+1

@IntelliJAmiya检查这个http://shaikhhamadali.blogspot.in/2013/07/set-brightness-of-imageincreasedecrease.html。只有图像不是屏幕。 – Steve

+0

了问题的实质。看到http://stackoverflow.com/questions/12891520/how-to-programmatically-change-contrast-of-a-bitmap-in-android –

回答

1

简单的方法是在imageView上叠加一层黑色,然后使用seekbar轻松节省时间的解决方案将叠加层的透明度映射出来。

,或者你可以使用下面的代码,以增强位图的亮度

/** 
* 
* @param bmp input bitmap 
* @param contrast 0..10 1 is default 
* @param brightness -255..255 0 is default 
* @return new bitmap 
*/ 
public static Bitmap changeBitmapContrastBrightness(Bitmap bmp, float contrast, float brightness) 
{ 
    ColorMatrix cm = new ColorMatrix(new float[] 
      { 
       contrast, 0, 0, 0, brightness, 
       0, contrast, 0, 0, brightness, 
       0, 0, contrast, 0, brightness, 
       0, 0, 0, 1, 0 
      }); 

    Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig()); 

    Canvas canvas = new Canvas(ret); 

    Paint paint = new Paint(); 
    paint.setColorFilter(new ColorMatrixColorFilter(cm)); 
    canvas.drawBitmap(bmp, 0, 0, paint); 

    return ret; 
} 

由于这个答案change the contrast and brightness of the bitmap

+0

所以,我们不能得到比原来更明亮的图片? –

+0

更明亮的图片??你是什​​么意思?你能详细说明一下吗? –

+0

@ArsenSench只是增加了一个方法,以增强该位图的对比度和亮度。 –