Android应用之涂鸦、changeColor

本文介绍了一个简单的Android涂鸦板应用程序实现,包括基本的颜色选择、画笔粗细调节及清除功能,并展示了如何通过触摸事件绘制线条及保存画作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

涂鸦

  • MainActivity
public class MainActivity extends Activity implements OnClickListener {
private View v_red;
private View v_green;
private View v_blue;
private SeekBar seekBar;
private Button clear;
private ImageView iv;
private Paint paint;
private Bitmap newBitmap;
private Canvas canvas;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        newBitmap = Bitmap.createBitmap(320, 320, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(newBitmap);
        canvas.drawColor(Color.WHITE);//设置导出时的背景色

        v_blue = findViewById(R.id.blue);
        v_green = findViewById(R.id.green);
        v_red = findViewById(R.id.red);
        seekBar = (SeekBar) findViewById(R.id.sb);
        clear = (Button) findViewById(R.id.clear);
        iv = (ImageView) findViewById(R.id.iv);

        iv.setOnTouchListener(new OnTouchListener() {

            int startX,startY,stopX,stopY;
            @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_UP://离开

                    break;
                case MotionEvent.ACTION_MOVE://移动
                    stopX = (int) event.getX();
                    stopY = (int) event.getY();
                    canvas.drawLine(startX, startY, stopX, stopY, paint);
                    iv.setImageBitmap(newBitmap);
                    startX = (int) event.getX();
                    startY = (int) event.getY();
                    break;


                }
                return true;
            }
        });

        v_red.setOnClickListener(this);
        v_green.setOnClickListener(this);
        v_blue.setOnClickListener(this);
        clear.setOnClickListener(this);

        paint = new Paint();
        paint.setStrokeWidth(10);
        paint.setColor(Color.BLACK);


        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                paint.setStrokeWidth(progress);
                Toast.makeText(MainActivity.this, "画笔宽度为"+progress, 0).show();

            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
            }
        });

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.red:
            paint.setColor(Color.RED);
            Toast.makeText(MainActivity.this, "当前画笔颜色为红色", 0).show();
            break;
        case R.id.green:
            paint.setColor(Color.GREEN);
            Toast.makeText(MainActivity.this, "当前画笔颜色为绿色", 0).show();
            break;
        case R.id.blue:
            paint.setColor(Color.BLUE);
            Toast.makeText(MainActivity.this, "当前画笔颜色为蓝色", 0).show();
            break;
        case R.id.clear:
            canvas.drawColor(Color.WHITE);
            iv.setImageBitmap(newBitmap);
            break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.save) {
            try {
                File file = new File(Environment.getExternalStorageDirectory(),SystemClock.currentThreadTimeMillis()+".jpg");
                FileOutputStream stream = new FileOutputStream(file);
                newBitmap.compress(CompressFormat.JPEG, 100, stream);
                Toast.makeText(MainActivity.this, "保存成功", 0).show();
                //设置广播,使系统重新检查sdcard
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                intent.setData(Uri.fromFile(file));
                sendBroadcast(intent);
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "保存失败", 0).show();
            }
        }
        return super.onOptionsItemSelected(item);
    }

}
  • activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择画笔颜色" />
    <RelativeLayout 
         android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <View 
            android:id="@+id/red"
            android:background="#ff0000"
             android:layout_width="50dp"
             android:layout_height="50dp"/>
        <View 
            android:id="@+id/green"
            android:layout_toRightOf="@id/red"
            android:background="#00ff00"
             android:layout_width="50dp"
             android:layout_height="50dp"/>
        <View 
            android:id="@+id/blue"
             android:layout_toRightOf="@id/green"
            android:background="#0000ff"
             android:layout_width="50dp"
             android:layout_height="50dp"/>
       <Button 
            android:layout_alignParentRight="true"
           android:id="@+id/clear"
           android:text="Clear"
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
           />
    </RelativeLayout >

    <SeekBar
        android:id="@+id/sb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="20"
        android:progress="5" />

    <ImageView 
         android:id="@+id/iv"
         android:src="@drawable/ic_launcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>
  • menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/save"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
</menu>

修改图片的色调

  • MainActivity
public class MainActivity extends Activity {
    private SeekBar seekBar;
    private ImageView iv;
    private Canvas canvas;
    private Paint paint;
    private Bitmap bitmap,newBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = (SeekBar) findViewById(R.id.sb);
        iv = (ImageView) findViewById(R.id.iv);
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int result = (int) (seekBar.getProgress()/50.0f);

                ColorMatrix cm = new ColorMatrix();
                cm.set(new float[] {
                    1*result, 0, 0, 0, 0,   //红
                    0, 1, 0, 0, 0,          //绿
                    0, 0, 1, 0, 0,          //蓝
                    0, 0, 0, 1, 0           //透明度
                });
                paint.setColorFilter(new ColorMatrixColorFilter(cm));
                canvas.drawBitmap(bitmap, new Matrix(), paint);
                iv.setImageBitmap(newBitmap);

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
            }
        });
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bb);
        newBitmap  = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        canvas = new Canvas(newBitmap);
        paint = new Paint();
        canvas.drawBitmap(bitmap, new Matrix(), paint);
        iv.setImageBitmap(newBitmap);
    }
}
  • activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="青&lt;-------------------------------&gt;红" />

    <SeekBar
        android:id="@+id/sb"
        android:max="100"
        android:progress="50"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ImageView 
        android:id="@+id/iv"
        android:src="@drawable/ic_launcher"
         android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值