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

Android實(shí)現(xiàn)簡單畫圖畫板

 更新時(shí)間:2021年01月05日 08:43:19   作者:yu-Knight  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單畫圖畫板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

效果如圖:

布局文件:

<RelativeLayout 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"
 tools:context=".MainActivity">
 
 <ImageView
  android:id="@+id/iv"
  android:layout_width="600px"
  android:layout_height="900px"
  android:layout_alignParentLeft="true"
  android:layout_alignParentRight="true"
  android:layout_alignParentStart="true" />
 
 <LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_gravity="center_horizontal"
  android:orientation="horizontal">
 
  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:onClick="red"
   android:text="紅色" />
 
  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:onClick="green"
   android:text="綠色"
    />
 
  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:onClick="brush"
   android:text="刷子"
   />
 
  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:onClick="eraser"
   android:text="橡皮擦"
    />
 
  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:onClick="save"
   android:text="保存" />
 
  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:onClick="cancel"
   android:text="取消" />
 </LinearLayout>
 
</RelativeLayout>

MainActivity.java

package com.example.yulongji.android10;
 
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
 
 
public class MainActivity extends Activity {
 
 private ImageView iv;
 //原圖
 private Bitmap bitsrc;
 //拷貝圖
 private Bitmap bitcopy;
 private Canvas canvas;
 private Paint paint;
 private int startX;
 private int startY;
 
 
 
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  iv = (ImageView) findViewById(R.id.iv);
  setBitmap();
 
  // 設(shè)置觸摸偵聽
  iv.setOnTouchListener(new View.OnTouchListener() {
 
   // 觸摸屏幕時(shí),觸摸事件產(chǎn)生時(shí),此方法調(diào)用
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    switch (action) {
     // 用戶手指摸到屏幕
     case MotionEvent.ACTION_DOWN:
      startX = (int) event.getX();
      startY = (int) event.getY();
      break;
     // 用戶手指正在滑動(dòng)
     case MotionEvent.ACTION_MOVE:
      int x = (int) event.getX();
      int y = (int) event.getY();
      canvas.drawLine(startX, startY, x, y, paint);
      // 每次繪制完畢之后,本次繪制的結(jié)束坐標(biāo)變成下一次繪制的初始坐標(biāo)
      startX = x;
      startY = y;
      iv.setImageBitmap(bitcopy);
      break;
     // 用戶手指離開屏幕
     case MotionEvent.ACTION_UP:
      break;
 
    }
    // true:告訴系統(tǒng),這個(gè)觸摸事件由我來處理
    // false:告訴系統(tǒng),這個(gè)觸摸事件我不處理,這時(shí)系統(tǒng)會(huì)把觸摸事件傳遞給imageview的父節(jié)點(diǎn)
    return true;
   }
  });
 }
 
 public void setBitmap() {
  // 加載畫畫板的背景圖
 bitsrc = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
  // 創(chuàng)建圖片副本
  // 1.在內(nèi)存中創(chuàng)建一個(gè)與原圖一模一樣大小的bitmap對(duì)象,創(chuàng)建與原圖大小一致的白紙
  bitcopy = Bitmap.createBitmap(bitsrc.getWidth(), bitsrc.getHeight(),
    bitsrc.getConfig());
  // 2.創(chuàng)建畫筆對(duì)象
  paint = new Paint();
  // 3.創(chuàng)建畫板對(duì)象,把白紙鋪在畫板上
  canvas = new Canvas(bitcopy);
  // 4.開始作畫,把原圖的內(nèi)容繪制在白紙上
  canvas.drawBitmap(bitsrc, new Matrix(), paint);
  // 5.將繪制的圖放入imageview中
  iv.setImageBitmap(bitcopy);
 }
 
 public void red(View view){
  paint.setColor(Color.RED);
 }
 public void green(View view){
  paint.setColor(Color.GREEN);
 }
 public void brush(View view){
  paint.setStrokeWidth(8);
 }
 public void cancel(View view){
  setBitmap();
 }
 public void eraser(View view){
  paint.setColor(Color.rgb(243,243,243));
 
  paint.setStrokeWidth(30);
 }
 
 public void save(View view){
  String path = Environment.getExternalStorageDirectory() + "/" + "2.png";
  File file = new File(path);
  try {
   FileOutputStream fos = new FileOutputStream(file);
   bitcopy.compress(Bitmap.CompressFormat.PNG, 100, fos);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  // 發(fā)送sd卡就緒廣播
  Intent intent = new Intent();
  intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
  intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
  sendBroadcast(intent);
 }
 
}

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

相關(guān)文章

  • Android 如何獲取設(shè)備唯一標(biāo)識(shí)

    Android 如何獲取設(shè)備唯一標(biāo)識(shí)

    這篇文章主要介紹了Android 如何獲取設(shè)備唯一標(biāo)識(shí),幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Android自定義View實(shí)現(xiàn)shape圖形繪制

    Android自定義View實(shí)現(xiàn)shape圖形繪制

    這篇文章主要為大家詳細(xì)介紹了Android使用自定義View實(shí)現(xiàn)shape圖形繪制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 解決EditText不顯示光標(biāo)的三種方法(總結(jié))

    解決EditText不顯示光標(biāo)的三種方法(總結(jié))

    下面小編就為大家?guī)硪黄鉀QEditText不顯示光標(biāo)的三種方法(總結(jié))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Kotlin協(xié)程低級(jí)api startCoroutine與ContinuationInterceptor

    Kotlin協(xié)程低級(jí)api startCoroutine與ContinuationInterceptor

    這篇文章主要為大家介紹了Kotlin協(xié)程低級(jí)api startCoroutine與ContinuationInterceptor示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android中TelephonyManager類的用法案例詳解

    Android中TelephonyManager類的用法案例詳解

    這篇文章主要介紹了Android中TelephonyManager類的用法,以獲取Android手機(jī)硬件信息為例詳細(xì)分析了TelephonyManager類的使用技巧,需要的朋友可以參考下
    2015-09-09
  • Android ListView之setEmptyView正確使用方法

    Android ListView之setEmptyView正確使用方法

    這篇文章主要介紹了Android ListView之setEmptyView正確使用方法的相關(guān)資料,希望通過本文能幫助到大家使用該方法,需要的朋友可以參考下
    2017-09-09
  • Android中使用自定義ViewGroup的總結(jié)

    Android中使用自定義ViewGroup的總結(jié)

    本篇文章主要介紹了Android中使用自定義ViewGroup的總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • 實(shí)例講解建立Android項(xiàng)目

    實(shí)例講解建立Android項(xiàng)目

    在本篇內(nèi)容中我們給大家整理了關(guān)于建立Android項(xiàng)目的步驟和相關(guān)知識(shí)點(diǎn),有興趣的讀者們學(xué)習(xí)下。
    2019-03-03
  • Android圓角設(shè)置方法看著一篇文章就夠了

    Android圓角設(shè)置方法看著一篇文章就夠了

    我們?cè)趯?shí)際工作中,android經(jīng)常有需要實(shí)現(xiàn)圓角的場景,下面這篇文章主要給大家介紹了關(guān)于Android圓角設(shè)置方法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì)需要的朋友可以參考下
    2023-02-02
  • 淺談?wù)凙ndroid 圖片選擇器

    淺談?wù)凙ndroid 圖片選擇器

    近段時(shí)間有項(xiàng)目要求寫一個(gè)類似于微信發(fā)送圖片時(shí),用來選擇照片的一個(gè)圖片瀏覽器。相信有很多網(wǎng)友也有這樣的需求,這里分享給大家
    2015-12-12

最新評(píng)論

宁波市| 和政县| 威海市| 滨州市| 古丈县| 韶关市| 台中县| 建宁县| 大城县| 金华市| 大竹县| 乌海市| 图们市| 弥渡县| 石景山区| 大庆市| 平谷区| 盐源县| 辉县市| 香格里拉县| 棋牌| 津南区| 察哈| 金湖县| 江孜县| 南昌县| 大余县| 武清区| 武夷山市| 绥宁县| 黄石市| 青阳县| 定边县| 鹤岗市| 德兴市| 曲阳县| 明水县| 龙江县| 修水县| 灌阳县| 承德县|