国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android畫畫板的制作方法

2019-10-22 18:29:12
字體:
來源:轉載
供稿:網友

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

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 手勢識別器整體思路:因為我是圖片是作畫,實際是對圖片進行修改,起到畫圖的效果1.原圖,白紙,畫筆,畫板2.根據手勢識別進行作畫 */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);    //創建白紙,寬,高,圖片的參數     bitmap = Bitmap.createBitmap(bmSrc.getWidth(), bmSrc.getHeight(), bmSrc.getConfig());    //創建畫板,參數是白紙對象    canvas = new Canvas(bitmap);    //創建畫筆    paint = new Paint();    //在紙上作畫    iv=(ImageView)findViewById(R.id.iv);    canvas.drawBitmap(bmSrc,new Matrix(), paint);    //-----------------手勢識別器和畫筆結合的知識-------------------  //給控件設置手勢適配器,可以得到用戶在這個控件上所做的手勢    iv.setOnTouchListener(new View.OnTouchListener() {      //當用戶手在這個控件時,指的就是用戶的手對控件滑動,按下,松開的三種場景,自動回調      @Override      public boolean onTouch(View view, MotionEvent motionEvent) {        switch (motionEvent.getAction()){          case MotionEvent.ACTION_DOWN://按下時回調一次            //獲取用戶手指按下時的坐標            startx = (int) motionEvent.getX();            starty = (int) motionEvent.getY();            break;          case MotionEvent.ACTION_MOVE://手指滑動時,不停地調用            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://松開時回調一次            break;        }        //事情分發機制        //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寫入數據權限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 稻城县| 慈溪市| 怀柔区| 台中市| 南溪县| 辰溪县| 苍山县| 东乌| 乐安县| 陆丰市| 新民市| 湄潭县| 舒兰市| 杭州市| 古田县| 永春县| 厦门市| 防城港市| 壶关县| 拜泉县| 什邡市| 屏东市| 德庆县| 治多县| 株洲市| 霸州市| 麦盖提县| 湘乡市| 汉源县| 海晏县| 平顺县| 曲松县| 遵义市| 怀安县| 鞍山市| 长沙县| 长宁县| 绵阳市| 双柏县| 尚义县| 乾安县|