您的位置:首页 > 移动开发 > Android开发

android 随手涂鸦

2015-07-13 16:26 375 查看
在平时开发中,有时会有类似于画画板随手涂鸦的程序,可以在手机上画出一些图案,写字等,此程序重在思路,接下来直接上代码

1、布局文件 activity_main.xml

<RelativeLayout 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" >

<ImageView

android:id="@+id/imageview"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true" />

<Button

android:id="@+id/clear"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_toLeftOf="@+id/save"

android:text="@string/clear" />

<Button

android:id="@+id/save"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:layout_marginLeft="5dp"

android:text="@string/save" />

</RelativeLayout>

2、MainActivity.java代码

package com.ilemi.paint;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.text.SimpleDateFormat;

import java.util.Date;

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.os.Bundle;

import android.os.Environment;

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 MainActivity extends Activity implements OnClickListener

{

private ImageView imageview=null;

private Button clear=null;

private Button save=null;

private MyOntouchListener touchlistener;//手势监听事件

private Bitmap bitmap;

private Canvas canvas;//画板

private Paint paint;//画笔

private int DownX=0;//按下时的横坐标

private int DownY=0;//按下时有竖坐标

private int MoveX=0;//移动时有横坐标

private int MoveY=0;//移动时有竖坐标

private Toast toast=null;//消息提示框

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initview();

}

private void initview()

{

if(imageview==null)

{

imageview=(ImageView)this.findViewById(R.id.imageview);

}

if(clear==null)

{

clear=(Button)this.findViewById(R.id.clear);

}

if(save==null)

{

save=(Button)this.findViewById(R.id.save);

}

if(touchlistener==null)

{

touchlistener=new MyOntouchListener();

}

imageview.setOnTouchListener(touchlistener);

clear.setOnClickListener(this);

save.setOnClickListener(this);

}

private class MyOntouchListener implements OnTouchListener

{

@Override

public boolean onTouch(View v,MotionEvent event)

{

int event_action=event.getAction();

switch(event.getAction())

{

case MotionEvent.ACTION_DOWN:

initbitmap();

DownX=(int)event.getX();

DownY=(int)event.getY();

break;

case MotionEvent.ACTION_MOVE:

MoveX=(int)event.getX();

MoveY=(int)event.getY();

if(canvas==null)

{

initbitmap();

}

canvas.drawLine(DownX,DownY,MoveX,MoveY,paint);

DownX=MoveX;

DownY=MoveY;

imageview.setImageBitmap(bitmap);

break;

case MotionEvent.ACTION_UP:

break;

}

return true;

}

}

@Override

public void onClick(View v)

{

if(bitmap==null)

{

return ;

}

switch(v.getId())

{

case R.id.clear:

canvas.drawColor(Color.GREEN);

imageview.setImageBitmap(bitmap);

break;

case R.id.save:

save();

break;

}

}

private void initbitmap()

{

if(bitmap==null)

{

bitmap=Bitmap.createBitmap(imageview.getWidth(),imageview.getHeight(),Config.ARGB_4444);

canvas=new Canvas(bitmap);

paint=new Paint();

paint.setColor(Color.RED);

paint.setStrokeWidth(5);

canvas.drawColor(Color.GREEN);

}

imageview.setImageBitmap(bitmap);

}

private void save()

{

//获取sd卡路经

File CacheDir=Environment.getExternalStorageState().endsWith(Environment.MEDIA_MOUNTED)? Environment.getExternalStorageDirectory():getCacheDir();

Date date=new Date();

SimpleDateFormat sdf=new SimpleDateFormat("yyyy_MM_DD_HH_mm_ss");

String file_name=sdf.format(date)+".jpg";

File CacheFile=new File(CacheDir,file_name);

try

{

FileOutputStream fos=new FileOutputStream(CacheFile);

boolean issuccessful=bitmap.compress(CompressFormat.JPEG,100,fos);

show(issuccessful);

}

catch(FileNotFoundException e)

{

e.printStackTrace();

show(false);

}

}

private void show(boolean issuccessful)

{

toast=Toast.makeText(MainActivity.this,issuccessful?R.string.save_successful:R.string.save_fail,0);

if(!toast.getView().isShown())

{

toast.show();

}

}

}

3、strings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="app_name">随手涂鸦</string>

<string name="hello_world">Hello world!</string>

<string name="clear">清除</string>

<string name="save">保存</string>

<string name="save_successful">保存成功</string>

<string name="save_fail">保存失败</string>

</resources>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: