Android 我的第一个画图板

效果图如下:


xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context="com.xiaoming.drawtest.DrawAndSave">
    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/btRed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:text="red"
            android:background="#f00"/>
        <Button
            android:id="@+id/btGreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="gre"
            android:background="#0f0"
            android:layout_toRightOf="@+id/btRed"
            android:layout_alignParentBottom="true"/>
        <Button
            android:id="@+id/btBlue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00f"
            android:text="blue"
            android:layout_alignParentBottom="true"
            android:layout_toRightOf="@+id/btGreen"/>
        <Button
            android:id="@+id/btChangeSize"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="PSize"
            android:background="#fff"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"/>
        <Button
            android:id="@+id/btEraser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="eraser"
            android:background="#fff"
            android:layout_marginTop="20dp"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/btChangeSize"/>
        <Button
            android:id="@+id/btClear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="clear"
            android:background="#fff"
            android:layout_marginTop="20dp"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/btEraser"/>
        <Button
            android:id="@+id/btSave2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="save"
            android:background="#fff"
            android:layout_marginTop="20dp"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/btEraser"/>
        <Button
            android:id="@+id/btToActivity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Trun"
            android:background="#fff"
            android:layout_marginTop="20dp"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/btSave2"/>
    </RelativeLayout>
</FrameLayout>
源码:
package com.xiaoming.drawtest;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;

public class DrawAndSave extends Activity {

    private ImageView iv;
    private Bitmap bitMap;
    private Canvas canvas;
    private Paint paint;
    private Button btRed;
    private Button btGreen;
    private Button btBlue;
    private Button btChangeSize;//改变画笔大小按钮
    private Button btEraser;//简单擦除效果按钮
    private Button btClear;
    private Button btSave2;//保存图片按钮
    private Button btToActivity;
    private static int size = 0;//连续点击设置不同paint size


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_draw_and_save);
        DrawAndSave.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        initView();
        setListener();
        bitMap = Bitmap.createBitmap(getWindowManager().getDefaultDisplay().getWidth(),
                getWindowManager().getDefaultDisplay().getHeight(),
                Bitmap.Config.ARGB_8888);

        canvas = new Canvas(bitMap);
        canvas.drawColor(Color.BLACK);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(3);
        iv.setImageBitmap(bitMap);

        iv.setOnTouchListener(new View.OnTouchListener() {
            int startX;
            int startY;
            int newX;
            int newY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        startX = (int)event.getX();
                        startY = (int)event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        newX = (int)event.getX();
                        newY = (int)event.getY();
                        canvas.drawLine(startX,startY,newX,newY,paint);
                        iv.invalidate();
                        startX = (int)event.getX();
                        startY = (int)event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        newX = (int)event.getX();
                        newY = (int)event.getY();
                        canvas.drawLine(startX,startY,newX,newY,paint);
                        iv.invalidate();
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }
    public void initView(){
        btRed = (Button)findViewById(R.id.btRed);
        btGreen = (Button)findViewById(R.id.btGreen);
        btBlue = (Button)findViewById(R.id.btBlue);
        btChangeSize = (Button)findViewById(R.id.btChangeSize);
        btEraser = (Button)findViewById(R.id.btEraser);
        btClear = (Button)findViewById(R.id.btClear);
        btSave2 = (Button)findViewById(R.id.btSave2);
        btToActivity = (Button)findViewById(R.id.btToActivity);
        iv = (ImageView)findViewById(R.id.iv);
    }
    public void setListener(){
        MyListener myListener = new MyListener();
        btRed.setOnClickListener(myListener);
        btGreen.setOnClickListener(myListener);
        btBlue.setOnClickListener(myListener);
        btChangeSize.setOnClickListener(myListener);
        btEraser.setOnClickListener(myListener);
        btClear.setOnClickListener(myListener);
        btSave2.setOnClickListener(myListener);
        btToActivity.setOnClickListener(myListener);
    }
    public class MyListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btRed:
                    paint.setColor(Color.RED);
                    break;
                case R.id.btGreen:
                    paint.setColor(Color.GREEN);
                    break;
                case R.id.btBlue:
                    paint.setColor(Color.BLUE);
                    break;
                case R.id.btChangeSize:
                    int tempSize = 0;
                    tempSize = (size++)%3;
                    switch (tempSize){
                        case 0:paint.setStrokeWidth(3);break;
                        case 1:paint.setStrokeWidth(10);break;
                        case 2:paint.setStrokeWidth(18);break;
                    }
                    break;
                case R.id.btEraser:
                    paint.setColor(Color.BLACK);
                    paint.setStrokeWidth(10);
                    break;
                case R.id.btClear:
                    bitMap = Bitmap.createBitmap(getWindowManager().getDefaultDisplay().getWidth(),
                            getWindowManager().getDefaultDisplay().getHeight(),
                            Bitmap.Config.ARGB_8888);
                    canvas.setBitmap(bitMap);
                    canvas.drawColor(Color.BLACK);
                    iv.setImageBitmap(bitMap);
                    break;
                case R.id.btSave2:
                    canvas.save(Canvas.ALL_SAVE_FLAG);
                    canvas.restore();
                    File file = new File("/sdcard/kidgame/");
                    if(!file.exists())
                        file.mkdirs();
                    try{
                        FileOutputStream fileOutputStream = new FileOutputStream(file.getPath()+"/"+System.currentTimeMillis()+".jpg");
                        bitMap.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);
                        fileOutputStream.close();
                        Toast.makeText(DrawAndSave.this,"saved!",Toast.LENGTH_SHORT).show();
                    }catch (Exception e){
                        Toast.makeText(DrawAndSave.this,"not save!",Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }
                    break;
                case R.id.btToActivity:
                    Intent intent = new Intent(DrawAndSave.this,DrawBaseImage.class);
                    startActivity(intent);
                    break;
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值