郑州app开发画画板案例。布局代码是三个button和一个imagesview下面是图片。

72ace45e754d6a57c7f14fe92cf3fadf.png

布局代码就不展示了。下面是java代码。

package cn.xhhkj.image;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Matrix;

import android.graphics.Paint;

import android.graphics.Point;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.widget.ImageView;

import android.graphics.Bitmap.CompressFormat;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "结果是";

private Canvas canvas;

private Paint paint;

private ImageView iv_image;

private Bitmap copybm;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

init();

indo();

}

public void init(){

iv_image = findViewById(R.id.iv_image);

}

public void indo(){

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),

R.drawable.c);

copybm = Bitmap.createBitmap(bitmap.getWidth(),

bitmap.getHeight(), bitmap.getConfig());

canvas = new Canvas(copybm);

paint = new Paint();

canvas.drawBitmap(bitmap, new Matrix(), paint);

iv_image.setImageBitmap(copybm);

iv_image.setOnTouchListener(new View.OnTouchListener() {

private float startX;

private float startY;

@Override

public boolean onTouch(View v, MotionEvent event) {

int action = event.getAction();

switch (action){

case MotionEvent.ACTION_DOWN:

startX = event.getX();

startY = event.getY();

break;

case MotionEvent.ACTION_MOVE:

float x = event.getX();

float y = event.getY();

canvas.drawLine(startX, startY, x, y, paint);

iv_image.setImageBitmap(copybm);

startX = x;

startY = y;

break;

case MotionEvent.ACTION_UP:

break;

default:

break;

}

return true;

}

});

}

public void changeColor(View view) {

paint.setColor(Color.RED);

}

public void bold(View view) {

paint.setStrokeWidth(5);

}

public void save(View view) {

Log.d(TAG,"保存");

File file = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".png");

FileOutputStream fos;

try {

fos = new FileOutputStream(file);

copybm.compress(CompressFormat.PNG, 100, fos);

//            Intent intent = new Intent();

//            intent.setAction(Intent.ACTION_MEDIA_MOUNTED);

//            intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));

//            sendBroadcast(intent);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

以上代码完成后,还需要在清单里面添加权限,android.permission.WRITE_EXTERNAL_STORAGE。这个是往手机存储权限。

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐