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

android 實(shí)現(xiàn)在照片上繪制涂鴉的方法

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

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

一、選擇圖片

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

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

<?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>

根據(jù)機(jī)型設(shè)置縮放比例

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

效果圖如下

android 在照片上繪制涂鴉

二、繪制涂鴉

實(shí)現(xiàn)代碼如下:

 @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 在照片上繪制涂鴉

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

實(shí)現(xiàn)代碼如下:

try {

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

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

四、擦除涂鴉筆跡

實(shí)現(xiàn)代碼如下:

 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);

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

以上這篇android 實(shí)現(xiàn)在照片上繪制涂鴉的方法就是小編分享給大家的全部?jī)?nèi)容了,希望這篇文章能夠?yàn)樾』锇閭兲峁┮恍椭?,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

吉木萨尔县| 伊吾县| 宜川县| 雷山县| 德昌县| 湘乡市| 苍溪县| 三都| 南雄市| 双鸭山市| 广宗县| 榕江县| 松潘县| 禄劝| 乐平市| 新竹市| 丹寨县| 永仁县| 东乡| 辽宁省| 武乡县| 特克斯县| 大石桥市| 昆山市| 桃园市| 汶川县| 甘洛县| 剑阁县| 壤塘县| 滕州市| 卢龙县| 西平县| 金山区| 石家庄市| 岳池县| 周宁县| 新竹市| 新津县| 滁州市| 当雄县| 乌兰浩特市|