Android开发笔记之简易画画板的制作( 二 )

Android开发笔记之简易画画板的制作

文章插图
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {public void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubint progress=seekBar.getProgress();float count=progress/2f;paint.setStrokeWidth(count);}public void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {// TODO Auto-generated method stub}});
触摸事件及其画线操作
iv.setOnTouchListener(new OnTouchListener() {public boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:startx=event.getX();starty=event.getY();break;case MotionEvent.ACTION_MOVE:stopx=event.getX();stopy=event.getY();canvas.drawLine(startx, starty, stopx, stopy, paint);startx=event.getX();starty=event.getY();iv.setImageBitmap(copybitmap);break;case MotionEvent.ACTION_UP:break;}return true;}});
这样基本事件都完成了 。
Android开发笔记之简易画画板的制作

文章插图
.class
package com.verney.board;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageView;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.TextView;public class MainActivity extends Activity {private Bitmap basebitmap,copybitmap;private Bitmap bitmap;private Canvas canvas;private Paint paint;ImageView iv;TextView huaxian;private float startx,starty;SeekBar seekbar;public void red(View view){paint.setColor(Color.RED);}public void green(View view){paint.setColor(Color.GREEN);}public void blue(View view){paint.setColor(Color.BLUE);}public void xiangpi(View view){paint.setColor(Color.WHITE);}protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv=(ImageView) findViewById(R.id.iv);seekbar=(SeekBar) findViewById(R.id.seekbar);//basebitmap=BitmapFactory.decodeResource(getResources(), R.drawable.);//canvasinit();seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {public void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubint progress=seekBar.getProgress();float count=progress/2f;paint.setStrokeWidth(count);}public void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {// TODO Auto-generated method stub}});iv.setOnTouchListener(new OnTouchListener() {public boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:startx=event.getX();starty=event.getY();break;case MotionEvent.ACTION_MOVE:float stopx=event.getX();float stopy=event.getY();canvas.drawLine(startx, starty, stopx, stopy, paint);startx=event.getX();starty=event.getY();iv.setImageBitmap(copybitmap);break;case MotionEvent.ACTION_UP:break;}return true;}});}private void init() {basebitmap=BitmapFactory.decodeResource(getResources(), R.drawable.bg);copybitmap=Bitmap.createBitmap(basebitmap.getWidth(),basebitmap.getHeight(),basebitmap.getConfig());canvas=new Canvas(copybitmap);//设置画板颜色canvas.drawColor(Color.WHITE);paint=new Paint();//设置画笔颜色//paint.setColor(Color.RED);//设置画笔粗细paint.setStrokeWidth(0);iv.setImageBitmap(copybitmap);//设置画笔风格//paint.setStyle(Style.STROKE);}}