Android学习笔记进阶之在图片上涂鸦(能清屏)

 

HandWritingActivity.java

package xiaosi.handWriting;
  
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
  
public class HandWritingActivity extends Activity
{
     /** Called when the activity is first created. */
     private HandWrite handWrite = null ;
     private Button clear = null ;
     @Override
     public void onCreate(Bundle savedInstanceState)
     {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
          
         handWrite = (HandWrite)findViewById(R.id.handwriteview);
         clear = (Button)findViewById(R.id.clear);
         clear.setOnClickListener( new clearListener());
     }
       
      
     private class clearListener implements OnClickListener{
  
         public void onClick(View v)
         {
             handWrite.clear();
         }
     }
}

HandWrite.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
public class HandWrite extends View
{
     private Paint paint = null ;
     private Bitmap originalBitmap = null ;
     private Bitmap new1Bitmap = null ;
     private Bitmap new2Bitmap = null ;
     private float clickX = 0 ,clickY = 0 ;
     private float startX = 0 ,startY = 0 ;
     private boolean isMove = true ;
     private boolean isClear = false ;
     private int color = Color.GREEN;
     private float strokeWidth = 2 .0f;
     public HandWrite(Context context, AttributeSet attrs)
     {
         super (context, attrs);
         originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.girl_a);
         new1Bitmap = Bitmap.createBitmap(originalBitmap);
     }
      
  
     public void clear(){
         isClear = true ;
         new2Bitmap = Bitmap.createBitmap(originalBitmap);
         invalidate();
     }
     public void setstyle( float strokeWidth){
         this .strokeWidth = strokeWidth;
     }
     @Override
     protected void onDraw(Canvas canvas)
     {
         super .onDraw(canvas);
         canvas.drawBitmap(HandWriting(new1Bitmap), 0 , 0 , null );
          
     }
  
     public Bitmap HandWriting(Bitmap originalBitmap)
     {
         Canvas canvas = null ;
          
         if (isClear){
             canvas = new Canvas(new2Bitmap);
         }
         else {
             canvas = new Canvas(originalBitmap);
         }
         paint = new Paint();
         paint.setStyle(Style.STROKE);
         paint.setAntiAlias( true );
         paint.setColor(color);
         paint.setStrokeWidth(strokeWidth);
         if (isMove){
             canvas.drawLine(startX, startY, clickX, clickY, paint);
         }
          
         startX = clickX;
         startY = clickY;
          
         if (isClear){
             return new2Bitmap;
         }
         return originalBitmap;
     }
  
     @Override
     public boolean onTouchEvent(MotionEvent event)
     {
         clickX = event.getX();
         clickY = event.getY();
         if (event.getAction() == MotionEvent.ACTION_DOWN){
              
             isMove = false ;
             invalidate();
             return true ;
         }
         else if (event.getAction() == MotionEvent.ACTION_MOVE){
              
             isMove = true ;
             invalidate();
             return true ;
         }
          
         return super .onTouchEvent(event);
     }
     
}

main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent"
     android:orientation= "vertical" >
  
     <xiaosi.handWriting.HandWrite
         android:id= "@+id/handwriteview"
         android:layout_width= "fill_parent"
         android:layout_height= "380dp" />
  
     <LinearLayout
         android:layout_width= "fill_parent"
         android:layout_height= "fill_parent"
         android:orientation= "horizontal"
         android:gravity= "center_horizontal" >
  
         <Button
             android:id= "@+id/clear"
             android:layout_width= "200dp"
             android:layout_height= "wrap_content"
             android:text= "清屏" />
          
          
     </LinearLayout>
</LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值