前几篇都涉及到了Canvas,Paint,Bitmap的结合使用,这里就不多说了~
现在我要写的是画画板这个项目~
项目效果:
1.画笔的颜色的随意更改
2.画笔的大小也可以随着拖动条的拖动而改变
3.橡皮擦清除画的效果
4.画板的清空
效果图:
布局文件很简单:
<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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#ff0000"
android:id="@+id/red"/>
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#00ff00"
android:id="@+id/green"/>
<View
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#0000ff"
android:id="@+id/blue"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="橡皮擦"
android:onClick="click"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空"
android:onClick="clear"/>
</LinearLayout>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seek"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"
android:background="@drawable/bg"/>
</LinearLayout>
MainActivity:
public class MainActivity extends Activity implements OnClickListener{
private View red,green,blue;
private SeekBar seek;
private ImageView iv;
private Bitmap baseBitmap,copyBitmap;
private Canvas canvas;
private Paint paint;
private int startX,startY;
public void click(View view){
paint.setColor(Color.BLACK);
paint.setStrokeWidth(40);
}
public void clear(View view){
canvas.drawColor(Color.BLACK);
paint.setColor(Color.BLACK);
/*baseBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.bg);
iv.setImageBitmap(baseBitmap);*/
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
red=(View)findViewById(R.id.red);
green=(View)findViewById(R.id.green);
blue=(View)findViewById(R.id.blue);
iv=(ImageView) findViewById(R.id.iv);
seek=(SeekBar) findViewById(R.id.seek);
red.setOnClickListener(this);
blue.setOnClickListener(this);
green.setOnClickListener(this);
baseBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.bg);
copyBitmap=Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), baseBitmap.getConfig());
canvas=new Canvas(copyBitmap);
//画板的颜色
canvas.drawColor(Color.BLACK);
paint=new Paint();
//画笔的颜色
paint.setColor(Color.RED);
iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN://落下
startX=(int)event.getX();
startY=(int)event.getY();
break;
case MotionEvent.ACTION_MOVE://移动
int stopX=(int)event.getX();
int stopY=(int)event.getY();
canvas.drawLine(startX, startY, stopX, stopY, paint);
startX=(int)event.getX();
startY=(int)event.getY();
iv.setImageBitmap(copyBitmap);
break;
case MotionEvent.ACTION_UP://离开
default:
break;
}
return true;
}
});
seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
int progress=seekBar.getProgress();
//使拖动条的取值为0f-2f,满足我们的取值要求
float count=progress/50f;
paint.setStrokeWidth(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.red:
paint.setColor(Color.RED);
break;
case R.id.green:
paint.setColor(Color.GREEN);
break;
case R.id.blue:
paint.setColor(Color.BLUE);
break;
default:
break;
}
}
}
因为代码不是很麻烦,所以现在不上传源码
有想要源码的可以联系我,到时候传上来~