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

Android畫畫板的制作方法

 更新時間:2017年08月28日 11:59:48   作者:heibuke  
這篇文章主要為大家詳細(xì)介紹了Android畫畫板的制作方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android畫畫板展示的具體代碼,供大家參考,具體內(nèi)容如下

main.xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
  android:layout_width="match_parent" android:layout_height="match_parent"
  tools:context="com.example.demo.MainActivity">

  <ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/bg"
    />

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    >
    <Button
      android:id="@+id/red"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="紅色"
      android:onClick="onplay"
      />
    <Button
      android:id="@+id/green"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="綠色"
      android:onClick="onplay"
      />
    <Button
      android:id="@+id/root"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="刷子"
      android:onClick="onplay"
      />
    <Button
      android:id="@+id/save"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="保存"
      android:onClick="onplay"
      />
    <Button
      android:id="@+id/finish"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="涂漆"
      android:onClick="onplay"
      />
  </LinearLayout>

</RelativeLayout>

main布局

/*
畫板canvas  畫板paint 手勢識別器
整體思路:因?yàn)槲沂菆D片是作畫,實(shí)際是對圖片進(jìn)行修改,起到畫圖的效果
1.原圖,白紙,畫筆,畫板
2.根據(jù)手勢識別進(jìn)行作畫

 */
public class MainActivity extends AppCompatActivity {
private Bitmap bitmap;
  private Canvas canvas;
private ImageView iv;
  private int startx;
  private int starty;
  private Paint paint;
  private Bitmap bmSrc;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //加載原圖
    bmSrc = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
    //創(chuàng)建白紙,寬,高,圖片的參數(shù)
     bitmap = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());
    //創(chuàng)建畫板,參數(shù)是白紙對象
    canvas = new Canvas(bitmap);
    //創(chuàng)建畫筆
    paint = new Paint();
    //在紙上作畫
    iv=(ImageView)findViewById(R.id.iv);
    canvas.drawBitmap(bmSrc,new Matrix(), paint);
    //-----------------手勢識別器和畫筆結(jié)合的知識-------------------
  //給控件設(shè)置手勢適配器,可以得到用戶在這個控件上所做的手勢
    iv.setOnTouchListener(new View.OnTouchListener() {


      //當(dāng)用戶手在這個控件時,指的就是用戶的手對控件滑動,按下,松開的三種場景,自動回調(diào)
      @Override
      public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()){
          case MotionEvent.ACTION_DOWN://按下時回調(diào)一次
            //獲取用戶手指按下時的坐標(biāo)
            startx = (int) motionEvent.getX();
            starty = (int) motionEvent.getY();
            break;
          case MotionEvent.ACTION_MOVE://手指滑動時,不停地調(diào)用
            int newx = (int) motionEvent.getX();
            int newy = (int) motionEvent.getY();
            //在背景圖畫線
            canvas.drawLine(startx,starty,newx,newy, paint);
            startx=newx;
            starty=newy;
            iv.setImageBitmap(bitmap);
            break;
          case MotionEvent.ACTION_UP://松開時回調(diào)一次

            break;
        }
        //事情分發(fā)機(jī)制
        //true:iv處理該觸摸事件
        //false:iv不處理該觸摸事件,事件傳遞給上一級
        return true;
      }
    });
  }
  public void onplay(View view){
   switch (view.getId()){
     case R.id.red:
       paint.setColor(Color.RED);
       break;
     case R.id.green:
       paint.setColor(Color.GREEN);
       break;
     case R.id.root:
       paint.setStrokeWidth(5);
       break;
     case R.id.save:
       if(SaveViewUtil.saveScreen(iv)){
         Toast.makeText(this, "截圖成功", Toast.LENGTH_SHORT).show();
       }else{
         Toast.makeText(this, "截圖失敗,請檢查sdcard是否可用", Toast.LENGTH_SHORT).show();
       }
       break;
     //涂漆
     case R.id.finish:
       canvas.drawRect(new Rect(0,0,width,height), paint);
       break;
     }

   }
  }

這是一個把畫的圖存儲SD卡的工具類

public class SaveViewUtil {
  
  private static final File rootDir = new File(Environment.getExternalStorageDirectory()+File.separator);

  /**保存截圖的方法*/
  public static boolean saveScreen(View view){
   //判斷sdcard是否可用
   if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
     return false;
   }
   if(!rootDir.exists()){
     rootDir.mkdir();
   }
   view.setDrawingCacheEnabled(true);
   view.buildDrawingCache();
   Bitmap bitmap = view.getDrawingCache();
   try {
     bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(new File(rootDir,System.currentTimeMillis()+".jpg")));
     return true;
   } catch (FileNotFoundException e) {
     e.printStackTrace();
     return false;
   }finally{
     view.setDrawingCacheEnabled(false);
     bitmap = null;
   }
  }
}
<!-- 往SDCard寫入數(shù)據(jù)權(quán)限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android開發(fā)之ListView的簡單用法及定制ListView界面操作示例

    Android開發(fā)之ListView的簡單用法及定制ListView界面操作示例

    這篇文章主要介紹了Android開發(fā)之ListView的簡單用法及定制ListView界面操作,結(jié)合實(shí)例形式分析了Android ListView界面布局相關(guān)操作技巧,需要的朋友可以參考下
    2019-04-04
  • Android異步消息機(jī)制詳解

    Android異步消息機(jī)制詳解

    這篇文章主要為大家詳細(xì)介紹了Android異步消息機(jī)制的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android自定義輸入框提示功能

    Android自定義輸入框提示功能

    這篇文章主要為大家詳細(xì)介紹了Android自定義輸入框提示功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • 在Android開發(fā)中使用自定義組合控件的例子

    在Android開發(fā)中使用自定義組合控件的例子

    這篇文章主要介紹了在Android開發(fā)中使用自定義組合控件的例子,作者根據(jù)例子總結(jié)到了實(shí)現(xiàn)父類的構(gòu)造方法等基本要點(diǎn),具有一定參考價值,需要的朋友可以參考下
    2016-02-02
  • Android視頻懸浮窗口實(shí)現(xiàn)的示例代碼

    Android視頻懸浮窗口實(shí)現(xiàn)的示例代碼

    這篇文章主要介紹了Android視頻懸浮窗口實(shí)現(xiàn)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Android 全局異常捕獲實(shí)例詳解

    Android 全局異常捕獲實(shí)例詳解

    這篇文章主要介紹了 Android 全局異常捕獲實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android編程實(shí)現(xiàn)監(jiān)聽EditText變化的方法

    Android編程實(shí)現(xiàn)監(jiān)聽EditText變化的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)監(jiān)聽EditText變化的方法,涉及Android針對EditText的相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • android側(cè)滑菜單控件DrawerLayout使用方法詳解

    android側(cè)滑菜單控件DrawerLayout使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了android側(cè)滑菜單控件DrawerLayout的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android MQTT與WebSocket協(xié)議詳細(xì)講解

    Android MQTT與WebSocket協(xié)議詳細(xì)講解

    MQTT(消息隊(duì)列遙測傳輸)是ISO 標(biāo)準(zhǔn)(ISO/IEC PRF 20922)下基于發(fā)布/訂閱范式的消息協(xié)議。它工作在TCP/IP協(xié)議族上,是為硬件性能低下的遠(yuǎn)程設(shè)備以及網(wǎng)絡(luò)狀況糟糕的情況下而設(shè)計(jì)的發(fā)布/訂閱型消息協(xié)議
    2022-11-11
  • Android高仿微信對話列表滑動刪除效果

    Android高仿微信對話列表滑動刪除效果

    這篇文章主要為大家詳細(xì)介紹了Android高仿微信對話列表滑動刪除效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評論