這篇文章將為大家詳細講解有關Android中怎么對圖片的顏色進行處理,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="青--紅" /> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/red_seekbar"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="青--綠" /> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/green_seekbar"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="青--藍" /> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/blue_seekbar"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/im" android:src="@drawable/img_small_1"/> </LinearLayout>
activity:
public class MainActivity extends Activity implements OnSeekBarChangeListener{
private SeekBar red_sb,green_sb,blue_sb;
private ImageView imageView;
private Canvas canvas;
private Paint paint;
private Bitmap baseBitmap,copyBitmap;
private float red_vector,green_vector,blue_vector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
red_sb=(SeekBar) findViewById(R.id.red_seekbar);
green_sb=(SeekBar) findViewById(R.id.green_seekbar);
blue_sb=(SeekBar) findViewById(R.id.blue_seekbar);
imageView=(ImageView) findViewById(R.id.im);
red_sb.setOnSeekBarChangeListener(this);
green_sb.setOnSeekBarChangeListener(this);
blue_sb.setOnSeekBarChangeListener(this);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
int progress=seekBar.getProgress();
float count=progress/50f;//使拖動條的取值為0f-2f,滿足我們的取值要求
switch (seekBar.getId()) {
case R.id.red_seekbar:
this.red_vector=count;
break;
case R.id.green_seekbar:
this.green_vector=count;
break;
case R.id.blue_seekbar:
this.blue_vector=count;
break;
default:
break;
}
//主題代碼
baseBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.img_small_1);
copyBitmap=Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), baseBitmap.getConfig());
canvas=new Canvas(copyBitmap);
Matrix matrix=new Matrix();
paint=new Paint();
//vector:取值范圍(0-2)
float[] colors=new float[]{
red_vector,0,0,0,0,
0,green_vector,0,0,0,
0,0,blue_vector,0,0,
0,0,0,1,0};
paint.setColorFilter(new ColorMatrixColorFilter(colors));
canvas.drawBitmap(baseBitmap, matrix, paint);
imageView.setImageBitmap(copyBitmap);
}
}關于Android中怎么對圖片的顏色進行處理就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。