欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

一、選擇圖片

這個(gè)道長有必要說一下,在繪制涂鴉時(shí),筆畫會(huì)根據(jù)設(shè)置ImageView的大小和屏幕的尺寸(不是像素)產(chǎn)生誤差。這個(gè)道長暫時(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)在照片上繪制涂鴉的方法就是小編分享給大家的全部內(nèi)容了,希望這篇文章能夠?yàn)樾』锇閭兲峁┮恍椭?,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android開發(fā)之關(guān)于項(xiàng)目

    Android開發(fā)之關(guān)于項(xiàng)目

    本文是此系列文章的第二篇,給大家介紹的是項(xiàng)目相關(guān)的內(nèi)容,非常的細(xì)致全面,有需要的小伙伴可以參考下
    2016-02-02
  • Android使用Handler和Message更新UI

    Android使用Handler和Message更新UI

    這篇文章主要介紹了Android使用Handler和Message更新UI的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android自定義View onDraw()方法會(huì)調(diào)用兩次的問題解決

    Android自定義View onDraw()方法會(huì)調(diào)用兩次的問題解決

    這篇文章主要介紹了Android自定義View onDraw()方法會(huì)調(diào)用兩次的問題解決,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-01-01
  • 基于android布局中的常用占位符介紹

    基于android布局中的常用占位符介紹

    下面小編就為大家分享一篇基于android布局中的常用占位符介紹,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Android實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的全過程

    Android實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的全過程

    在Android開發(fā)中,經(jīng)常需要設(shè)置控件的背景顏色或者圖片的src顏色,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)背景顏色滑動(dòng)漸變效果的相關(guān)資料,本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • 如何利用Flutter實(shí)現(xiàn)酷狗流暢Tabbar效果

    如何利用Flutter實(shí)現(xiàn)酷狗流暢Tabbar效果

    這篇文章主要給大家介紹了關(guān)于如何利用Flutter實(shí)現(xiàn)酷狗流暢Tabbar效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-02-02
  • Android遍歷所有文件夾和子目錄搜索文件

    Android遍歷所有文件夾和子目錄搜索文件

    為了準(zhǔn)確搜索文件,大家可以采取什么方法查找文件,本文為大家介紹Android遍歷所有文件夾和子目錄實(shí)現(xiàn)文件搜索功能,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android EditText限制輸入字?jǐn)?shù)的方法

    Android EditText限制輸入字?jǐn)?shù)的方法

    這篇文章主要介紹了Android EditText限制輸入字?jǐn)?shù)的方法,涉及Android針對EditText文本與字符串操作相關(guān)技巧,需要的朋友可以參考下
    2016-01-01
  • Android 如何查看Wifi密碼

    Android 如何查看Wifi密碼

    這篇文章主要介紹了Android 如何查看Wifi密碼的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Android狀態(tài)欄白底黑字的示例代碼

    Android狀態(tài)欄白底黑字的示例代碼

    這篇文章主要介紹了Android狀態(tài)欄白底黑字的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10

最新評(píng)論