本文參考文獻(xiàn):《瘋狂Android講義(第2版)》
自定義視圖:
package com.toby.personal.testlistview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by toby on 2017/4/10.
*/
public class DrawView extends View {
private final static int VIEW_WIDTH = 720;
private final static int VIEW_HEIGHT = 1280;
private float preX;
private float preY;
private Path path;
private Paint paint = null;
private Paint bmpPaint = null;
private Bitmap cacheBitmap = null;
private Canvas cacheCanvas = null;
public DrawView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
cacheBitmap = Bitmap.createBitmap(VIEW_WIDTH, VIEW_HEIGHT, Bitmap.Config.ARGB_8888);
cacheCanvas = new Canvas();
path = new Path();
cacheCanvas.setBitmap(cacheBitmap);
paint = new Paint(Paint.DITHER_FLAG);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setAntiAlias(true);
paint.setDither(true);
bmpPaint = new Paint();
}
@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);
path.reset();
break;
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);
canvas.drawPath(path, paint);
}
public void setColor(int color){
paint.setColor(color);
}
public void setStrokeWidth(float width){
paint.setStrokeWidth(width);
}
public void setMaskFilter(MaskFilter maskFilter){
paint.setMaskFilter(maskFilter);
}
}
自定義 menu 資源文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<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>
<item
android:id="@+id/width_1"
android:title="@string/width_1" />
<item
android:id="@+id/width_3"
android:title="@string/width_3" />
<item
android:id="@+id/width_5"
android:title="@string/width_5" />
</group>
</menu>
</item>
<item
android:id="@+id/blur"
android:title="@string/blur" />
<item
android:id="@+id/emboss"
android:title="@string/emboss" />
</menu>
主布局文件的內(nèi)容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorGray"
android:orientation="vertical"
android:id="@+id/container"
>
<com.toby.personal.testlistview.DrawView
android:id="@+id/drawView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
主程序文件的內(nèi)容:
package com.toby.personal.testlistview;
import android.graphics.BlurMaskFilter;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
final private static String TAG = "Toby_Test";
EmbossMaskFilter emboss;
BlurMaskFilter blur;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emboss = new EmbossMaskFilter(new float[]{1.5f, 1.5f, 1.5f}, 0.6f, 6, 4.2f);
blur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.test_menu01, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
DrawView drawView = (DrawView) findViewById(R.id.drawView);
switch (item.getItemId()){
case R.id.red:
drawView.setColor(Color.RED);
item.setChecked(true);
break;
case R.id.green:
drawView.setColor(Color.GREEN);
item.setChecked(true);
break;
case R.id.blue:
drawView.setColor(Color.BLUE);
item.setChecked(true);
break;
case R.id.width_1:
drawView.setStrokeWidth(1);
break;
case R.id.width_3:
drawView.setStrokeWidth(3);
break;
case R.id.width_5:
drawView.setStrokeWidth(5);
break;
case R.id.blur:
drawView.setMaskFilter(blur);
break;
case R.id.emboss:
drawView.setMaskFilter(emboss);
break;
}
return true;
}
}
運行效果: