码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-05-11 14:53:06      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:涂鸦   view   android   图片处理   图片   

原图:

技术分享

效果图:

技术分享


代码如下:

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情绪控_





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

标签:涂鸦   view   android   图片处理   图片   

原文地址:http://blog.csdn.net/hai_qing_xu_kong/article/details/45643093

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!