Skip to content

Commit

Permalink
Merge pull request #400 from qingtian1993/master
Browse files Browse the repository at this point in the history
#90 #43 lab4
  • Loading branch information
zengsn committed Apr 26, 2016
2 parents 2760a7d + 93d0aa5 commit cfe6326
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
51 changes: 45 additions & 6 deletions app/src/main/res/layout/activity_com1314080901225.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<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"
tools:context=".Com1314080901225Activity" >
tools:context=".Com1314080901225Activity"
android:orientation="vertical"
>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:src="@drawable/c"
android:src="@drawable/bg"
/>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"
android:onClick="red"

/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="black"
android:text="黑色"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="brush"
android:text="加粗"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="save"
android:text="保存"
/>



</LinearLayout>

</RelativeLayout>
</LinearLayout>

0 comments on commit cfe6326

Please sign in to comment.