如此隨意的android塗鴉工具

要實現塗鴉這個功能,首先需要以下幾步:

1、要實現一個DrawingView類,這個類繼承View;

2、重寫其父類的構造方法,並初始化塗鴉的畫筆、畫布的畫筆和路徑;

3、重寫onDraw方法,在這個方法中主要進行塗鴉的繪製工作;

4、重寫onTouchEvent事件,在這個方法計算當前的畫筆的左邊,規劃畫筆的路徑,調用invalidate()方法不斷調用onDraw方法進行繪製;

5、自定義setPattern方法設置畫筆的描邊效果;

6、在MainActivity以及其佈局文件中引入自定義的DrawdingView對象;

代碼如下:

public class DrawingView extends View {

    private Path drawPath;
    private Paint drawPaint;
    private Paint canvasPaint;
    private int paintColor = 0xFF000000;
    private Bitmap canvasBitmap;
    private Canvas drawCanvas;

    public DrawingView(Context context) {
        super(context,null);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        canvasBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
        drawCanvas = new Canvas(canvasBitmap);
    }

    public DrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupDrawing();
    }

    private void setupDrawing() {
        drawPaint = new Paint();
        drawPath = new Path();
        drawPaint.setColor(paintColor);
        drawPaint.setAntiAlias(true);
        drawPaint.setStrokeWidth(50);
        drawPaint.setStyle(Paint.Style.STROKE);
        drawPaint.setStrokeJoin(Paint.Join.ROUND);
        drawPaint.setStrokeCap(Paint.Cap.ROUND);
        canvasPaint = new Paint(Paint.DITHER_FLAG);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(canvasBitmap,0,0,canvasPaint);
        canvas.drawPath(drawPath,drawPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float touchX = event.getX();
        float touchY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:drawPath.moveTo(touchX,touchY);break;
            case MotionEvent.ACTION_MOVE:drawPath.lineTo(touchX,touchY);break;
            case MotionEvent.ACTION_UP:drawPath.lineTo(touchX, touchY);
                drawCanvas.drawPath(drawPath, drawPaint);
                drawPath.reset();break;
            default:return false;
        }
        invalidate();
        return true;
    }

    public void setPattern(String newPattern){
        invalidate();
        int patternId = getResources().getIdentifier(newPattern,"drawable","com.example.lucky.drawapp");
        Bitmap patternBMP = BitmapFactory.decodeResource(getResources(),patternId);
        BitmapShader patternBMPshader = new BitmapShader(patternBMP, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        drawPaint.setColor(0xFFFFFFFF);
        drawPaint.setShader(patternBMPshader);
    }
}
佈局文件:

<LinearLayout 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:background="#FFCCCCCC"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <com.example.lucky.drawapp.DrawingView
            android:id="@+id/draw_view"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="3dp"
            android:layout_weight="1"
            android:background="#FFFFFFFF"/>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical" >
            <!-- top row -->
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                <ImageButton
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_margin="2dp"
                    android:background="@drawable/pattern1"
                    android:contentDescription="pattern"
                    android:onClick="paintClicked"
                    android:tag="pattern1" />
                <ImageButton
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_margin="2dp"
                    android:background="@drawable/pattern2"
                    android:contentDescription="pattern"
                    android:onClick="paintClicked"
                    android:tag="pattern2" />
                <ImageButton
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_margin="2dp"
                    android:background="@drawable/pattern3"
                    android:contentDescription="pattern"
                    android:onClick="paintClicked"
                    android:tag="pattern3" />
                <ImageButton
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_margin="2dp"
                    android:background="@drawable/pattern4"
                    android:contentDescription="pattern"
                    android:onClick="paintClicked"
                    android:tag="pattern4" />
            </LinearLayout>
            <!-- bottom row -->
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                <ImageButton
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_margin="2dp"
                    android:background="@drawable/pattern5"
                    android:contentDescription="pattern"
                    android:onClick="paintClicked"
                    android:tag="pattern5" />
                <ImageButton
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_margin="2dp"
                    android:background="@drawable/pattern6"
                    android:contentDescription="pattern"
                    android:onClick="paintClicked"
                    android:tag="pattern6" />
                <ImageButton
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_margin="2dp"
                    android:background="@drawable/pattern7"
                    android:contentDescription="pattern"
                    android:onClick="paintClicked"
                    android:tag="pattern7" />
                <ImageButton
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_margin="2dp"
                    android:background="@drawable/pattern8"
                    android:contentDescription="pattern"
                    android:onClick="paintClicked"
                    android:tag="pattern8" />
            </LinearLayout>
        </LinearLayout>
    
    </LinearLayout>
Activity中的代碼:

public class MainActivity extends Activity {

    private DrawingView drawView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawView = (DrawingView) findViewById(R.id.draw_view);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void paintClicked(View view) {
        String pattern = view.getTag().toString();
        drawView.setPattern(pattern);
    }
}
效果圖如下:





發佈了43 篇原創文章 · 獲贊 25 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章