
代码中,重要地方都有注解
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="15dp"
android:layout_weight="1"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<com.example.gcc.okhttpl.richeditor.PaintView
android:id="@+id/activity_paint_pv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/activity_paint_undo"
style="@style/paint_menu_text_view_style"
android:text="撤销" />
<TextView
android:id="@+id/activity_paint_redo"
style="@style/paint_menu_text_view_style"
android:text="恢复" />
<TextView
android:id="@+id/activity_paint_color"
style="@style/paint_menu_text_view_style"
android:text="红色" />
<TextView
android:id="@+id/activity_paint_clear"
style="@style/paint_menu_text_view_style"
android:text="清空" />
<TextView
android:id="@+id/activity_paint_eraser"
style="@style/paint_menu_text_view_style"
android:text="橡皮擦" />
<TextView
android:id="@+id/activity_paint_save"
style="@style/paint_menu_text_view_style"
android:text="保存" />
</LinearLayout>
</LinearLayout>
Activity代码
public class PaintViewActivity extends AppCompatActivity {
private PaintView paintView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paint_view);
initView();
initMenu();
}
/**
* 初始化
*/
private void initView() {
paintView = (PaintView) findViewById(R.id.activity_paint_pv);
}
/**
* 初始化底部菜单
*/
private void initMenu() {
//撤销
menuItemSelected(R.id.activity_paint_undo, new MenuSelectedListener() {
@Override
public void onMenuSelected() {
ToastUtils.show(PaintViewActivity.this, "撤销");
paintView.undo();
}
});
//恢复
menuItemSelected(R.id.activity_paint_redo, new MenuSelectedListener() {
@Override
public void onMenuSelected() {
ToastUtils.show(PaintViewActivity.this, "恢复");
paintView.redo();
}
});
//颜色
menuItemSelected(R.id.activity_paint_color, new MenuSelectedListener() {
@Override
public void onMenuSelected() {
ToastUtils.show(PaintViewActivity.this, "红色");
paintView.setPaintColor(Color.RED);
}
});
//清空
menuItemSelected(R.id.activity_paint_clear, new MenuSelectedListener() {
@Override
public void onMenuSelected() {
ToastUtils.show(PaintViewActivity.this, "清空");
paintView.clearAll();
}
});
//橡皮擦
menuItemSelected(R.id.activity_paint_eraser, new MenuSelectedListener() {
@Override
public void onMenuSelected() {
ToastUtils.show(PaintViewActivity.this, "橡皮擦");
paintView.setEraserModel(true);
}
});
//保存
menuItemSelected(R.id.activity_paint_save, new MenuSelectedListener() {
@Override
public void onMenuSelected() {
String path = Environment.getExternalStorageDirectory().getPath()
+ File.separator + Strings.FILE_PATH + File.separator + Strings.CACHE_PATH;
String imgName = "paint.jpg";
if (paintView.saveImg(path,imgName)) {
ToastUtils.show(PaintViewActivity.this, "保存成功");
}
}
});
}
/**
* 选中底部 Menu 菜单项
*/
private void menuItemSelected(int viewId, final MenuSelectedListener listener) {
findViewById(viewId).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onMenuSelected();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
ToastUtils.cancel();
}
interface MenuSelectedListener {
void onMenuSelected();
}
}
这是橡皮擦使用PorterDuffXfermode踩坑模式

简单设置PaintView代码:
1. 首先把硬件加速打开,也就是把init()方法里,下面行代码注释掉:
//setBackgroundColor(Color.WHITE);
//setLayerType(View.LAYER_TYPE_SOFTWARE, null);
2.修改橡皮擦颜色
eraserPaint.setColor(Color.WHITE);
//eraserPaint.setColor(Color.TRANSPARENT);
//eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
这种模式即不会导致过分绘制,也不会有软件加速的坑,但前提是绘图板背景色彩是复古的
即使在使用过渡绘制模式的状况下,暂时觉得强度也可以,在低端机上也没有明显的卡顿感,绘制轨迹蛮跟手的。个人觉得这种绘图板并不需要SurfaceView
有错误,请指出
共勉 : )
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/ruanjian/article-120323-2.html
追求的不就是一种不平凡嘛
无限想念伟大领袖毛主席
期待