一起学android之给图片添加涂鸦(文字)(37)

原图:

技术分享

效果图:

技术分享


代码如下:

public class GraffitiView extends View  
{  
    private Paint paint = null;  
    /*
     * 源图
     */
    private Bitmap originalBitmap = null; 
    /*
     * 需要涂鸦的图片
     */
    private Bitmap new1Bitmap = null;  
    /*
     * 涂鸦之前
     */
    private Bitmap new2Bitmap = null; 
    /*
     * 触摸时的X、Y坐标
     */
    private float clickX = 0;
    private float clickY = 0;  
    /*
     * 每次绘制的起点
     */
    private float startX = 0;
    private float startY = 0;  
    /*
     * 是否进行绘制
     */
    private boolean isMove = true;  
    /*
     * 是否进行清空
     */
    private boolean isClear = false;  
    /*
     * 画笔颜色
     */
    private int color = Color.GREEN; 
    /*
     * 笔尖大小
     */
    private float strokeWidth = 2.0f;  
    
    public GraffitiView(Context context, AttributeSet attrs)  
    {  
        super(context, attrs);  
        originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hh).copy(Bitmap.Config.ARGB_8888, true);  
        new1Bitmap = Bitmap.createBitmap(originalBitmap);  
    }  
      
  
   
    
    @Override  
    protected void onDraw(Canvas canvas)  
    {  
        super.onDraw(canvas);  
        canvas.drawBitmap(HandWriting(new1Bitmap,isClear), 0, 0,null);  
          
    }  
  
    
    /**
     *  * 涂鸦的图案
     * @param originalBitmap
     * @param isClear 是否清空
     * @return 涂鸦的图案
     */
    public Bitmap HandWriting(Bitmap originalBitmap,boolean isClear)  
    {  
        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)  。
             */
            isMove = false;  
            invalidate();  
            return true;  
        }  
        else if(event.getAction() == MotionEvent.ACTION_MOVE){  
        	/*
        	 *当移动时,根据(isMove)进行绘制,从按下的坐标点起。
        	 */
            isMove = true;  
            invalidate();  
            return true;  
        }  
          
        return super.onTouchEvent(event);  
    }  
    
    
    /**
     * 清空
     */
    public void clear(){  
        isClear = true;  
        new2Bitmap = Bitmap.createBitmap(originalBitmap);  
        invalidate();  
    }  
    
    /**
     * 设置涂鸦线条的宽度
     * @param strokeWidth 宽度
     */
    public void setstyle(float strokeWidth){  
        this.strokeWidth = strokeWidth;  
    }  
    
    /**
     * 设置画笔颜色
     * @param color 颜色
     */
    public void setColor(int color){
    	this.color=color;
    }
     
}  



activity_main.xml:

<?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" >

    <com.example.tuya.view.GraffitiView
        android:id="@+id/handwriteview"
        android:layout_width="fill_parent"
        android:layout_height="500dp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/clear"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="清屏" />
    </LinearLayout>

</LinearLayout>



MainActivity:

public class MainActivity extends Activity {

	private GraffitiView handWrite = null;
	private Button clear = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		handWrite = (GraffitiView) findViewById(R.id.handwriteview);
		handWrite.setstyle(10);
		handWrite.setColor(Color.RED);
		clear = (Button) findViewById(R.id.clear);
		clear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				handWrite.clear();
			}
		});
	}

}



转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/45643093情绪控_





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。