From 93d0aa5cde50a699e12f3807429ae6b99b794183 Mon Sep 17 00:00:00 2001 From: qingtian1993 <15767979110@163.com> Date: Tue, 26 Apr 2016 20:12:12 +0800 Subject: [PATCH] =?UTF-8?q?#90=20#43=20=E8=AE=BE=E8=AE=A1=E7=95=8C?= =?UTF-8?q?=E9=9D=A2&&=E5=AE=8C=E6=88=90=E9=83=A8=E5=88=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Com1314080901225Activity.java | 90 +++++++++++++++++-- .../res/layout/activity_com1314080901225.xml | 51 +++++++++-- 2 files changed, 130 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901225/Com1314080901225Activity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901225/Com1314080901225Activity.java index af3bb2fa..40fbab1a 100644 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901225/Com1314080901225Activity.java +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901225/Com1314080901225Activity.java @@ -2,21 +2,101 @@ import android.os.Bundle; import android.app.Activity; +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.view.Menu; +import android.view.MotionEvent; +import android.view.View; +import android.view.View.OnTouchListener; +import android.widget.ImageView; +import android.widget.Toast; public class Com1314080901225Activity extends Activity { + private ImageView iv; + private int startX, startY, endX, endY; + Bitmap bitSrc, bitCopy; + Paint paint; + Canvas canvas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_com1314080901225); + iv = (ImageView) findViewById(R.id.iv); + Bitmap bitmap=copyBitmap(); + iv.setImageBitmap(bitmap); + iv.setOnTouchListener(new OnTouchListener() { + + @Override + public boolean onTouch(View v, MotionEvent event) { + // TODO Auto-generated method stub + switch (event.getAction()) { + // 手指按下 + case MotionEvent.ACTION_DOWN: + // 获取按下的xy + startX = (int) event.getX(); + startY = (int) event.getY(); + break; + + // 手指滑动 + case MotionEvent.ACTION_MOVE: + endX = (int) event.getX(); + endY = (int) event.getY(); + canvas.drawLine(startX, startY, endX, endY, paint); + iv.setImageBitmap(bitCopy); + + // 改变画笔再次的落点 + startX = endX; + startY = endY; + break; + + // 手指离开 + case MotionEvent.ACTION_UP: + + break; + + } + return true; + } + }); } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.com1314080901225, menu); - return true; + // 创建可编辑副本 + public Bitmap copyBitmap() { + bitSrc = BitmapFactory.decodeResource(getResources(), R.drawable.bg); + bitCopy = Bitmap.createBitmap(bitSrc.getWidth(), bitSrc.getHeight(), + bitSrc.getConfig()); + paint = new Paint(); + canvas = new Canvas(bitCopy); + canvas.drawBitmap(bitSrc, new Matrix(), paint); + return bitCopy; } + // 红色画笔 + public void red(View v) { + paint.setColor(Color.RED); + Toast.makeText(getApplicationContext(), "你选择了红色", 0).show(); + } + + // 黑色画笔 + public void black(View v) { + paint.setColor(Color.BLACK); + Toast.makeText(getApplicationContext(), "你选择了黑色", 0).show(); + } + + // 画笔加粗 + public void brush(View v) { + paint.setStrokeWidth(9); + Toast.makeText(getApplicationContext(), "画笔变粗了", 0).show(); + } + + //保存图片到sd卡 + public void save(View v){ + //保存图片到sd卡 + //Toast.makeText(getApplicationContext(), "保存图片", 0).show(); + } } diff --git a/app/src/main/res/layout/activity_com1314080901225.xml b/app/src/main/res/layout/activity_com1314080901225.xml index b04fbd3c..f346e325 100644 --- a/app/src/main/res/layout/activity_com1314080901225.xml +++ b/app/src/main/res/layout/activity_com1314080901225.xml @@ -1,4 +1,4 @@ - + tools:context=".Com1314080901225Activity" + android:orientation="vertical" + > + + +