筆記如下
如圖:
Video_2018-03-19_104911.gif
- 實(shí)現(xiàn)步驟:
首先是布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
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="com.chen.panel.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:onClick=""
android:id="@+id/color_red"
android:background="#ff0000"
android:layout_width="35dp"
android:layout_height="35dp"/>
<View
android:id="@+id/color_green"
android:background="#00ff00"
android:layout_width="35dp"
android:layout_height="35dp"/>
<View
android:id="@+id/color_blue"
android:background="#0000ff"
android:layout_width="35dp"
android:layout_height="35dp"/>
<View
android:id="@+id/color_yellow"
android:background="#ffff00"
android:layout_width="35dp"
android:layout_height="35dp"/>
<View
android:id="@+id/color_pink"
android:background="#ff99ff"
android:layout_width="35dp"
android:layout_height="35dp"/>
</LinearLayout>
<!--畫筆 -->
<SeekBar
android:id="@+id/skb_stroke"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/iv"
android:layout_width="200dp"
android:layout_height="200dp"
/>
<Button
android:onClick="save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存圖片"
/>
</LinearLayout>
調(diào)節(jié)顏色的方法
protected void onCreate(Bundle savedInstanceState) {
.............
color_red.setOnClickListener(this);
color_green.setOnClickListener(this);
color_blue.setOnClickListener(this);
color_pink.setOnClickListener(this);
color_yellow.setOnClickListener(this);
...........
}
//點(diǎn)擊換顏色的點(diǎn)擊事件
@Override
public void onClick(View v) {
switch(v.getId()){
//更改畫筆的顏色
case R.id.color_red:
paint.setColor(Color.RED);
break;
case R.id.color_yellow:
paint.setColor(Color.YELLOW);
break;
case R.id.color_green:
paint.setColor(Color.GREEN);
break;
case R.id.color_blue:
paint.setColor(Color.BLUE);
break;
case R.id.color_pink:
paint.setColor(0xffff99ff);
break;
default:
break;
}
}
ImageView的觸摸事件
float startX = 0;
float startY = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
//觸摸ImageView的回調(diào)
//觸摸的時(shí)候繪制我們的圖像,并且顯示
/*
Touch時(shí)有三種情況:event.getAction()
1.手指按下
2.手指移動(dòng)
3.手指抬起
*/
switch (event.getAction()){
case MotionEvent.ACTION_DOWN://1次
//1.手指按下
startX = event.getX();//觸摸的x坐標(biāo)
startY = event.getY();//觸摸的y坐標(biāo)
// startX = event.getRawX();
// startY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE://0-多次
//2.手指移動(dòng)
//繪制圖像,并顯示到ImageView上
float stopX = event.getX();
float stopY = event.getY();
// float stopX = event.getRawX();
// float stopY = event.getRawY();
System.out.println("x :" + stopX + " y : " + stopY);
canvas.drawLine(startX,startY,stopX,stopY,paint);
iv.setImageBitmap(bitmap);
startX = stopX;
startY = stopY;
break;
case MotionEvent.ACTION_UP://1次
//3.手指抬起
break;
default:
break;
}
return true;
}
seeBar的調(diào)節(jié)畫筆大小的stop事件
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//改變畫筆的粗細(xì)
int progress = seekBar.getProgress();
//paint.setStrokeWidth(progress);
paint.setStrokeWidth(10 * progress /100f + 0.5f);
}
完整源碼:
package com.chen.panel;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener, View.OnTouchListener {
private View color_red;
private View color_green;
private View color_yellow;
private View color_blue;
private View color_pink;
private SeekBar skb_stroke;
private ImageView iv;
private Bitmap bitmap;
private Canvas canvas;
private Paint paint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
color_red = findViewById(R.id.color_red);
color_green = findViewById(R.id.color_green);
color_yellow = findViewById(R.id.color_yellow);
color_blue = findViewById(R.id.color_blue);
color_pink = findViewById(R.id.color_pink);
skb_stroke = (SeekBar) findViewById(R.id.skb_stroke);
iv = (ImageView) findViewById(R.id.iv);
color_red.setOnClickListener(this);
color_green.setOnClickListener(this);
color_blue.setOnClickListener(this);
color_pink.setOnClickListener(this);
color_yellow.setOnClickListener(this);
skb_stroke.setOnSeekBarChangeListener(this);
iv.setOnTouchListener(this);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float density = metrics.density;
System.out.println(density);
//先準(zhǔn)備我們的畫板
bitmap = Bitmap.createBitmap((int) (200*density), (int) (200*density), Bitmap.Config.ARGB_8888);
//準(zhǔn)備畫紙
canvas = new Canvas(bitmap);
//畫布默認(rèn)是黑底的,所以要一開(kāi)始畫成白底的
canvas.drawColor(Color.WHITE);
//準(zhǔn)備畫筆
paint = new Paint();
}
private void requestPermission(){
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ //表示未授權(quán)時(shí)
//進(jìn)行授權(quán)
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
}else{
//調(diào)用打電話的方法
Toast.makeText(this, "權(quán)限已經(jīng)申請(qǐng)了.", Toast.LENGTH_SHORT).show();
}
}
//保存圖片
public void save(View view){
//File file = new File(Environment.getDataDirectory()+"/data/com.chen.panel/"+System.currentTimeMillis()+".jpg");
File file = new File(Environment.getExternalStorageDirectory()+"/"+System.currentTimeMillis()+".jpg");
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
//壓縮到流中
bitmap.compress(Bitmap.CompressFormat.JPEG,100,out);
} catch (Exception e) {
e.printStackTrace();
}finally {
if (out != null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
out = null;
}
}
}
//點(diǎn)擊換顏色的點(diǎn)擊事件
@Override
public void onClick(View v) {
switch(v.getId()){
//更改畫筆的顏色
case R.id.color_red:
paint.setColor(Color.RED);
break;
case R.id.color_yellow:
paint.setColor(Color.YELLOW);
break;
case R.id.color_green:
paint.setColor(Color.GREEN);
break;
case R.id.color_blue:
paint.setColor(Color.BLUE);
break;
case R.id.color_pink:
paint.setColor(0xffff99ff);
break;
default:
break;
}
}
//seebar實(shí)現(xiàn)
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//改變畫筆的粗細(xì)
int progress = seekBar.getProgress();
//paint.setStrokeWidth(progress);
paint.setStrokeWidth(10 * progress /100f + 0.5f);
}
float startX = 0;
float startY = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
//觸摸ImageView的回調(diào)
//觸摸的時(shí)候繪制我們的圖像,并且顯示
/*
Touch時(shí)有三種情況:event.getAction()
1.手指按下
2.手指移動(dòng)
3.手指抬起
*/
switch (event.getAction()){
case MotionEvent.ACTION_DOWN://1次
//1.手指按下
startX = event.getX();//觸摸的x坐標(biāo)
startY = event.getY();//觸摸的y坐標(biāo)
// startX = event.getRawX();
// startY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE://0-多次
//2.手指移動(dòng)
//繪制圖像,并顯示到ImageView上
float stopX = event.getX();
float stopY = event.getY();
// float stopX = event.getRawX();
// float stopY = event.getRawY();
System.out.println("x :" + stopX + " y : " + stopY);
canvas.drawLine(startX,startY,stopX,stopY,paint);
iv.setImageBitmap(bitmap);
startX = stopX;
startY = stopY;
break;
case MotionEvent.ACTION_UP://1次
//3.手指抬起
break;
default:
break;
}
return true;
}
}