zoukankan      html  css  js  c++  java
  • Android学习笔记进阶之在图片上涂鸦(能清屏)

    HandWritingActivity.Java

    1. package xiaosi.handWriting;  
    2.   
    3. import android.app.Activity;  
    4. import android.app.AlertDialog;  
    5. import android.content.DialogInterface;  
    6. import android.os.Bundle;  
    7. import android.view.View;  
    8. import android.view.View.OnClickListener;  
    9. import android.widget.Button;  
    10.   
    11. public class HandWritingActivity extends Activity  
    12. {  
    13.     /** Called when the activity is first created. */  
    14.     private HandWrite handWrite = null;  
    15.     private Button clear = null;  
    16.     @Override  
    17.     public void onCreate(Bundle savedInstanceState)  
    18.     {  
    19.         super.onCreate(savedInstanceState);  
    20.         setContentView(R.layout.main);  
    21.           
    22.         handWrite = (HandWrite)findViewById(R.id.handwriteview);  
    23.         clear = (Button)findViewById(R.id.clear);  
    24.         clear.setOnClickListener(new clearListener());  
    25.     }  
    26.        
    27.       
    28.     private class clearListener implements OnClickListener{  
    29.   
    30.         public void onClick(View v)  
    31.         {  
    32.             handWrite.clear();  
    33.         }  
    34.     }  
    35. }  


    HandWrite.java

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


    main.xml

      1. <?xml version="1.0" encoding="utf-8"?>  
      2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      3.     android:layout_width="fill_parent"  
      4.     android:layout_height="fill_parent"  
      5.     android:orientation="vertical" >  
      6.   
      7.     <xiaosi.handWriting.HandWrite  
      8.         android:id="@+id/handwriteview"  
      9.         android:layout_width="fill_parent"  
      10.         android:layout_height="380dp" />  
      11.   
      12.     <LinearLayout  
      13.         android:layout_width="fill_parent"  
      14.         android:layout_height="fill_parent"  
      15.         android:orientation="horizontal"  
      16.         android:gravity="center_horizontal" >  
      17.   
      18.         <Button  
      19.             android:id="@+id/clear"  
      20.             android:layout_width="200dp"  
      21.             android:layout_height="wrap_content"  
      22.             android:text="清屏" />  
      23.           
      24.           
      25.     </LinearLayout>  
      26. </LinearLayout> 
  • 相关阅读:
    Python XPath抓取小说《三国演义》《一》
    Python 爬虫学习路径
    Python爬虫练习:抓取笔趣阁小说(一)
    import win32api 安装pip install pypiwin32
    pycharm 选中多行,点back space键无法删除
    No module named 'wx'
    python GUI wxPython
    python -m pip install --upgrade pip 失败
    停更一年了,要不要回归呢,离开了运维行业也一年了。
    zabbix使用percona的mysql监控模板监控
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6722240.html
Copyright © 2011-2022 走看看