android多媒体编程--画画板
package com.example.huahuaban; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.ImageView; public class MainActivity extends Activity implements OnClickListener{ private ImageView img; Bitmap bitmapcopy; Paint paint; Bitmap bitmapsrc ; float startx =0; float starty = 0; private Canvas canvas; private Matrix mt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img = (ImageView) findViewById(R.id.img); findViewById(R.id.red).setOnClickListener(this); findViewById(R.id.gereen).setOnClickListener(this); findViewById(R.id.blue).setOnClickListener(this); findViewById(R.id.shua).setOnClickListener(this); findViewById(R.id.qingkong).setOnClickListener(this); findViewById(R.id.save).setOnClickListener(this); bitmapsrc = BitmapFactory.decodeResource(getResources(), R.drawable.bg); bitmapcopy = Bitmap.createBitmap(bitmapsrc.getWidth(), bitmapsrc.getHeight(), bitmapsrc.getConfig()); //画笔 paint = new Paint(); canvas = new Canvas(bitmapcopy); mt = new Matrix(); canvas.drawBitmap(bitmapsrc, mt, paint); img.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, 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); img.setImageBitmap(bitmapcopy); //把结束坐标的值赋给开始坐标 startx = x; starty = y; break; case MotionEvent.ACTION_UP: break; } return true; } }); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.red: paint.setColor(Color.RED); break; case R.id.gereen: paint.setColor(Color.GREEN); break; case R.id.blue: paint.setColor(Color.BLUE); break; case R.id.qingkong: paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR)); canvas.drawBitmap(bitmapsrc, mt, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC)); img.invalidate(); break; case R.id.shua: paint.setStrokeWidth(3.5f); break; case R.id.save: File file = new File("sdcard/ok1.png"); FileOutputStream out = null; try { out = new FileOutputStream(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //保存文件,参数分别为格式,压缩质量0-100,输出流 bitmapcopy.compress(CompressFormat.PNG, 100, out); //发送更新多媒体数据库的广播 Intent intent = new Intent(); intent.setAction(Intent.ACTION_MEDIA_MOUNTED); intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory())); sendBroadcast(intent); break; } } }
<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" tools:context=".MainActivity" android:orientation="vertical"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/bg"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/red" android:background="#FF0000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/gereen" android:background="#00FF00" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/blue" android:background="#3366FF" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/shua" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加粗" /> <Button android:id="@+id/qingkong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空" /> </LinearLayout> <Button android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存" /> </LinearLayout>