Android项目之随手涂鸦

一、软件的布局效果图


二、string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">随手涂鸦</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
</resources>

三、panel_activity_panel_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
        >
        <ImageView
        android:id="@+id/iv_image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
        <Button
         android:id="@+id/btn_save"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:layout_alignParentRight="true"
         android:text="保存" 
        />
          <Button
         android:id="@+id/btn_clear"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_toLeftOf="@id/btn_save"
         android:layout_alignTop="@id/btn_save"
         android:text="清除" 
        />
</RelativeLayout>

四、布局的响应事件代码Panel_MainActiviry.java

package com.example.panel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.R.color;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class Panel_MainActivity extends Activity implements OnClickListener {
ImageView imageView;//显示图片的控件对象
//画画的三要素
Canvas canvas;//画板
Bitmap  panel;//画纸
Paint paint;//画笔
private int downX;
private int downY;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.panel_activity_panel__main);
//开始写响应的代码
initView();
}

private void initView() {
// 获取组件
imageView=(ImageView) findViewById(R.id.iv_image);
   Button button=(Button) findViewById(R.id.btn_save);
   Button ClearButton=(Button) findViewById(R.id.btn_clear);
   //给imageView添加一个触摸的事件
   imageView.setOnTouchListener(new MyOnTouchListener());
   button.setOnClickListener(this);
   ClearButton.setOnClickListener(this);
}
/*MotionEvent.ACTION_DOWN 先按下
*MotionEvent.ACTION_MOVE 移动
*MotionEvent.ACTION_UP 最后抬起
* 按下和抬起只会触发一次,移动可以触发几次
*/
class MyOnTouchListener implements OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
//获取当前的事件,来判断一下,当前是按下,移动,抬起。
int action=event.getAction();//当前用户产生的动作
switch (action) {
case MotionEvent.ACTION_DOWN:
System.out.println("按下");
//初始化画纸
InitPanel();
//得到按下时的x、y值
downX=(int) event.getX();
downY=(int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
System.out.println("移动");
//取出移动下的x y值
int moveX=(int) event.getX();
int moveY=(int) event.getY();
//把按下的店和移动的点,两个点连成线
/*startX:按下的x
*startY:按下的y
*stopX:移动的x
*stopY:移动的y
*/
canvas.drawLine(downX, downY, moveX, moveY, paint);
imageView.setImageBitmap(panel);//把画完线的图片设置给imageView控件显示
//把按下的x和按下的y更新成移动后的x和y的值
downX=moveX;//将移动的x轴的值,赋值给按下的x轴的值,为了方便下一次进行连线
downY=moveY;//将移动的y轴的值,赋值给按下的y轴的值,为了方便下一次进行连线
break;
case MotionEvent.ACTION_UP:
System.out.println("抬起");
break;
default:
break;
}
return true;//true 消费当前的事件,false不消费事件,有其他人来处理
}

}
/*
* 初始化画纸
*/
private void InitPanel(){
if(panel==null){
//Canvas画板、bitmap画纸、画笔
            //创建一张空白的画纸,指定宽和高和ImageView空间的宽高一模一样
panel=Bitmap.createBitmap(imageView.getWidth(),imageView.getHeight(), Config.ARGB_8888);
//初始化一个画板。
canvas=new Canvas(panel);
//初始化一个画笔,指定画笔的颜色,指定画笔的画出来线的粗细
paint=new Paint();
paint.setColor(Color.RED);//指定画笔的颜色
paint.setStrokeWidth(5);//指定画笔画出来的线的宽度
//用画板向画纸画一个黄色的颜色
canvas.drawColor(Color.YELLOW);//这句代码执行完毕后,panel画纸的颜色就可以变成黄色
//把画纸给imageView控件来显示
imageView.setImageBitmap(panel);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.panel__main, menu);
return true;
}


@Override
public void onClick(View v) {
//v为当前点击的事件,可以判断当前点击的是什么事件
//当保存按钮点击触发该方法
int id=v.getId();
if(id==R.id.btn_save){
save_tupian();
}else if(id==R.id.btn_clear){
//将画板上的画的东西清空
canvas.drawColor(Color.WHITE);//把画纸画成白色
//把画白的图片设置给panel
imageView.setImageBitmap(panel);
}
 
}
private void save_tupian(){

//把画出来的图片保存起来
File cacheDir=Environment.getExternalStorageDirectory();
File cacheFile=new File(cacheDir, "gaozhen_0819");
System.out.println("路径:"+cacheFile.getPath());
try {
FileOutputStream out=new FileOutputStream(cacheFile);
boolean isSuccess=panel.compress(CompressFormat.JPEG, 100, out);
   if(isSuccess){
    Toast.makeText(this,"保存成功", 0).show();
    Toast.makeText(this,cacheFile.getPath(), 0).show();
   }else{
    Toast.makeText(this,"保存失败", 0).show();
   }
} catch (FileNotFoundException e) {

e.printStackTrace();
}
}
}

总结:学会提取,封装。以达到重用代码的效果。

五、permission问题

panel Manifest中设置。

你可能感兴趣的:(Android项目之随手涂鸦)