标签:android应用
package com.example.path3; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /*实现:采用双缓冲实现画图板*/ public class DrawView extends View { float preX; float preY; private Path path; public Paint paint=null; final int VIEW_WIDTH = 320; final int VIEW_HEIGHT = 480; Bitmap cacheBitmap = null; //定义一个内存中的图片,该图片将作为缓存区 Canvas cacheCanvas = null; //定义cacheCanvas上的Canvas对象 /*---------------------构造方法----------------------------*/ public DrawView(Context context, AttributeSet set) { super(context, set); path = new Path(); //1.创建一个与该View相同大小的缓存区 cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT, Config.ARGB_8888); //2.设置cacheCanvas将会绘制到内存中的cacheBitmap上 cacheCanvas = new Canvas(); cacheCanvas.setBitmap(cacheBitmap); //3.设置画笔(颜色、风格、反锯齿) paint = new Paint(Paint.DITHER_FLAG); //创建画笔 paint.setColor(Color.RED); //画笔颜色为红色 paint.setStyle(Paint.Style.STROKE); //设置画笔风格 paint.setAntiAlias(true); //反锯齿 paint.setDither(true); } /*---------------------监听触摸事件----------------------------*/ @Override public boolean onTouchEvent(MotionEvent event) { //获取拖动事件的发生位置 float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: path.moveTo(x, y); preX = x; preY = y; break; case MotionEvent.ACTION_MOVE: path.quadTo(preX, preY, x, y); preX = x; preY = y; break; case MotionEvent.ACTION_UP: cacheCanvas.drawPath(path, paint); //沿着路径绘制图形存放到cacheBitmap中 path.reset(); break; } invalidate(); return true; //返回true表明处理方法已经处理该事件 } // /*--------------------绘图-------------------------*/ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint bmpPaint = new Paint(); //a.将cacheBitmap绘制到该View组件上 canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint); //b.沿着path绘制 canvas.drawPath(path, paint); } }
package com.example.path3; import android.graphics.BlurMaskFilter; import android.graphics.Color; import android.graphics.EmbossMaskFilter; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; public class HandDraw extends ActionBarActivity { EmbossMaskFilter emboss; BlurMaskFilter blur; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); emboss = new EmbossMaskFilter(new float[] {1.5f,1.5f,1.5f}, 0.6f, 6,4.2f); blur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL); } //装载R.menu.hand_draw对应的菜单并添加到menu中 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.hand_draw, menu); return true; } //菜单被单击后的回调方法 @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); DrawView dv = (DrawView)findViewById(R.id.draw); //判断单击的是哪个菜单项并有针对性地作出响应 switch(id) { //颜色设置 case R.id.red: //红色 dv.paint.setColor(Color.RED); item.setChecked(true); break; case R.id.green: //绿色 dv.paint.setColor(Color.GREEN); item.setChecked(true); break; case R.id.blue: //红色 dv.paint.setColor(Color.BLUE); item.setChecked(true); break; //画笔大小设置 case R.id.width_1: //号画笔 dv.paint.setStrokeWidth(1); break; case R.id.width_2: //号画笔 dv.paint.setStrokeWidth(2); break; case R.id.width_5: //号画笔 dv.paint.setStrokeWidth(5); break; //模糊效果 case R.id.blur: dv.paint.setMaskFilter(blur); break; //浮雕效果 case R.id.emboss: dv.paint.setMaskFilter(emboss); break; } return super.onOptionsItemSelected(item); } }
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.path3.HandDraw" > <!-- 定义一组颜色选项菜单项 --> <item android:title="@string/color"> <menu> <group android:checkableBehavior="single"> <item android:id="@+id/red" android:title="@string/color_red"/> <item android:id="@+id/green" android:title="@string/color_green"/> <item android:id="@+id/blue" android:title="@string/color_blue"/> </group> </menu> </item> <!-- 定义一组笔触大小选项菜单项 --> <item android:title="@string/width"> <menu> <group android:checkableBehavior="single"> <item android:id="@+id/width_1" android:title="@string/width1"/> <item android:id="@+id/width_2" android:title="@string/width2"/> <item android:id="@+id/width_5" android:title="@string/width5"/> </group> </menu> </item> <!-- --> <item android:id="@+id/blur" android:title="@string/blur"/> <!-- 定义一组宽度选项菜单项 --> <item android:id="@+id/emboss" android:title="@string/emboss"/> </menu>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.path3.HandDraw" > <com.example.path3.DrawView android:id="@+id/draw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:width="320px" android:height="480px" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">简易画板</string> <string name="color">选择颜色</string> <string name="color_red">红色</string> <string name="color_green">绿色</string> <string name="color_blue">蓝色</string> <string name="width">画笔大小</string> <string name="width1">1号画笔</string> <string name="width2">2号画笔</string> <string name="width5">3号画笔</string> <string name="blur">模糊效果</string> <string name="emboss">浮雕效果</string> </resources>
标签:android应用
原文地址:http://blog.csdn.net/u012637501/article/details/43484505