最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android 實現在照片上繪制涂鴉的方法

 更新時間:2018年10月10日 09:50:30   作者:雨幕青山  
今天小編就為大家分享一篇android 實現在照片上繪制涂鴉的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

這個應該是簡易版的美圖秀秀(小伙伴們吐槽:你這也叫簡易版的??我們看著怎么不像啊……)。好吧,只是在圖片上繪制涂鴉,然后保存。

一、選擇圖片

這個道長有必要說一下,在繪制涂鴉時,筆畫會根據設置ImageView的大小和屏幕的尺寸(不是像素)產生誤差。這個道長暫時還沒有找到解決方法,只是規(guī)避了一下。

把ImageView設置為全屏,布局文件代碼如下

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_margin="5dp"
 android:orientation="vertical">

 <ImageView
  android:id="@+id/iv_draw_pic"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_gravity="center_horizontal"
  android:layout_margin="10dp" />

 <RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <Button
   android:id="@+id/btn_choose"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_margin="5dp"
   android:layout_weight="1"
   android:text="選擇照片" />

  <Button
   android:id="@+id/btn_save"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentRight="true"
   android:layout_margin="5dp"
   android:layout_weight="1"
   android:text="保存照片" />

  <Button
   android:id="@+id/btn_clear"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:layout_centerHorizontal="true"
   android:layout_margin="5dp"
   android:layout_weight="1"
   android:text="擦除筆跡" />
 </RelativeLayout>

</FrameLayout>

根據機型設置縮放比例

  switch (model){
   case "MI 4LTE":
    scale = 1.1f;
    break;
   case "HUAWEI NXT-AL10":
    scale = 1.5f;
    break;
  }

效果圖如下

android 在照片上繪制涂鴉

二、繪制涂鴉

實現代碼如下:

 @Override
 public boolean onTouch(View view, MotionEvent motionEvent) {
  int action = motionEvent.getAction();
  switch (action) {
   case MotionEvent.ACTION_CANCEL:
    break;
   case MotionEvent.ACTION_DOWN:
    downX = motionEvent.getX() * scale;
    downY = motionEvent.getY() * scale;
    break;
   case MotionEvent.ACTION_UP:
    upX = motionEvent.getX() * scale;
    upY = motionEvent.getY() * scale;
    canvas.drawLine(downX, downY, upX, upY, paint);
    iv_drawpicture.invalidate();
    break;
   case MotionEvent.ACTION_MOVE:
    upX = motionEvent.getX() * scale;
    upY = motionEvent.getY() * scale;
    canvas.drawLine(downX, downY, upX, upY, paint);
    iv_drawpicture.invalidate();
    downX = upX;
    downY = upY;
    break;
   default:
    break;
  }
  return true;
 }

效果圖如下:

android 在照片上繪制涂鴉

三、保存繪制涂鴉后的圖片

實現代碼如下:

try {

  Uri imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
  OutputStream os = getContentResolver().openOutputStream(imageUri);

  //compress方法將圖片轉換成JPG或者PNG格式
  drawBitmap.compress(Bitmap.CompressFormat.JPEG, 90, os);
  Toast.makeText(this, "Saved:" + imageUri.toString(), Toast.LENGTH_LONG).show();
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }

四、擦除涂鴉筆跡

實現代碼如下:

 drawBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
 canvas = createCanvas(drawBitmap);
 paint = createPaint();
 canvas.drawBitmap(bmp, 0, 0, paint);
 iv_drawpicture.setImageBitmap(drawBitmap);
 iv_drawpicture.setOnTouchListener(this);

在照片上繪制涂鴉暫時就到這里,功能比較單一。

以上這篇android 實現在照片上繪制涂鴉的方法就是小編分享給大家的全部內容了,希望這篇文章能夠為小伙伴們提供一些幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

绿春县| 尚义县| 如皋市| 石楼县| 綦江县| 湄潭县| 华阴市| 灌南县| 黄冈市| 惠来县| 望城县| 汕头市| 英山县| 嘉善县| 韶山市| 乌拉特前旗| 西林县| 垫江县| 沭阳县| 新源县| 岳普湖县| 休宁县| 阳春市| 平定县| 奈曼旗| 武清区| 永新县| 青岛市| 榕江县| 绥宁县| 修文县| 丹东市| 济南市| 汝州市| 名山县| 承德市| 宁波市| 扎囊县| 加查县| 柯坪县| 榆中县|