成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

怎么在Androidstudio項目中實現(xiàn)一個畫板功能-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關怎么在Android studio項目中實現(xiàn)一個畫板功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

成都創(chuàng)新互聯(lián)公司于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目網(wǎng)站制作、成都網(wǎng)站建設網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元廊坊做網(wǎng)站,已為上家服務,為廊坊各地企業(yè)和個人服務,聯(lián)系電話:13518219792

實現(xiàn)過程

項目布局很簡單

怎么在Android studio項目中實現(xiàn)一個畫板功能

讓我們來看代碼:首先聲明畫筆,畫板,和坐標

public class MainActivity extends AppCompatActivity{

 Paint paint;
 Canvas canvas;
 ImageView imageview;
 Bitmap bitmap,newbitmap;
 TextView tv_stroke;
 int startX, startY, endX, endY;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_my_paint_tools);
  LinearLayout ll_layout = findViewById(R.id.ll_layout);
  RadioGroup rg_color = findViewById(R.id.rg_color);

遍歷單選按鈕,當單選按鈕選中時,獲取單選按鈕顏色并將畫筆顏色設置當前按鈕的文本顏色,最后注意要設置畫筆寬度,以免在后面點橡皮擦的時候畫筆寬度調(diào)不回來

for (int i = 0;i<rg_color.getChildCount();i++){
   RadioButton rb = (RadioButton) rg_color.getChildAt(i);
   rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     if (buttonView.isChecked()){
      paint.setColor(buttonView.getTextColors().getDefaultColor()); 
      paint.setStrokeWidth(5);
     }
    }
   });
  }

首先創(chuàng)建一張空白圖片和一張灰色畫布,將圖片放在畫布上面

注冊觸摸監(jiān)聽事件,獲取鼠標按下時的坐標和鼠標移動后的坐標。在開始和結束之間畫一條直線并更新畫布圖片

 imageview.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    switch(event.getAction()){
     case MotionEvent.ACTION_DOWN:
      Log.i("MyPaintToolsActivity","ACTION_DOWN");

      
      startX = (int) (event.getX()/1.4);
      startY = (int) (event.getY()/1.4);
      break;
     case MotionEvent.ACTION_MOVE:
      Log.i("MyPaintToolsActivity","ACTION_MOVE");

      
      endX = (int) (event.getX()/1.4);
      endY = (int) (event.getY()/1.4);

      canvas.drawLine(startX,startY,endX,endY,paint);
      startX = (int) (event.getX()/1.4);
      startY = (int) (event.getY()/1.4);
      imageview.setImageBitmap(bitmap);
      break;
     case MotionEvent.ACTION_UP:
      Log.i("MyPaintToolsActivity","ACTION_UP");
      break;
    }
    imageview.invalidate();
    return true;
   }
  });

清屏的話就一行代碼 ,剩下的是重新生成一塊畫布

 Button btn_clear = findViewById(R.id.btn_clear);
  btn_clear.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    canvas.drawColor(0,PorterDuff.Mode.CLEAR);
    bitmap = Bitmap.createBitmap(888,1200,Bitmap.Config.ARGB_8888); 
    canvas = new Canvas(bitmap); 
    canvas.drawColor(Color.argb(100,0,0,0)); 
    paint = new Paint();
    paint.setStrokeWidth(5);
    paint.setAntiAlias(true);
    paint.setColor(Color.RED);
    canvas.drawBitmap(bitmap,new Matrix(),paint); 
    imageview.setImageBitmap(bitmap);   

   }
  });

呃,這里會把畫布擦掉…也就是擦成白色…

最后看看頁面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 android:id="@+id/ll_layout">
<!-- tools:context=".MyPaintToolsActivity">-->

 <ImageView

  android:id="@+id/imageview"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1" />
 <RadioGroup
  android:background="#747373"
  android:layout_width="match_parent"
  android:orientation="horizontal"
  android:id="@+id/rg_color"
  android:layout_height="wrap_content">

  <RadioButton
   android:id="@+id/rb_red"
   android:layout_width="wrap_content"
   android:layout_height="43dp"
   android:layout_weight="1"
   android:text="紅色"
   android:textColor="#FF0000"
   android:textSize="18sp" />

  <RadioButton
   android:id="@+id/rb_green"
   android:layout_width="wrap_content"
   android:layout_height="30dp"
   android:layout_weight="1"
   android:text="黑色"
   android:textColor="#000000"
   android:textSize="18sp" />

  <RadioButton
   android:id="@+id/rb_blue"
   android:layout_width="wrap_content"
   android:layout_height="30dp"
   android:layout_weight="1"
   android:text="白色"
   android:textColor="#FFFFFF"
   android:textSize="18sp" />

 </RadioGroup>
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:orientation="horizontal">
  <Button
   android:id="@+id/btn_clear"
   android:layout_width="wrap_content"
   android:layout_weight="1"
   android:layout_height="wrap_content"
   android:background="#000000"
   android:textColor="#FFFFFF"
   android:textSize="18sp"
   android:text="清除"/>
  <Button
   android:id="@+id/btn_eraser"
   android:layout_width="wrap_content"
   android:layout_weight="1"
   android:layout_height="wrap_content"
   android:textColor="#FFFFFF"
   android:textSize="18sp"
   android:background="#000000"
   android:text="擦除"/>

 </LinearLayout>

</LinearLayout>

看完上述內(nèi)容,你們對怎么在Android studio項目中實現(xiàn)一個畫板功能有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

網(wǎng)站標題:怎么在Androidstudio項目中實現(xiàn)一個畫板功能-創(chuàng)新互聯(lián)
路徑分享:http://jinyejixie.com/article8/hepop.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供服務器托管、網(wǎng)站改版外貿(mào)建站、軟件開發(fā)、標簽優(yōu)化、云服務器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)
惠州市| 方城县| 兴海县| 莱州市| 津南区| 闻喜县| 闻喜县| 镇安县| 巴青县| 莱州市| 黑山县| 巍山| 龙里县| 女性| 布拖县| 漳平市| 延安市| 尤溪县| 陆良县| 普兰店市| 万山特区| 靖宇县| 湖南省| 大方县| 临汾市| 屏南县| 宁乡县| 眉山市| 从江县| 曲水县| 牟定县| 白银市| 本溪市| 闽侯县| 长汀县| 霍林郭勒市| 通许县| 平定县| 宁强县| 建阳市| 子长县|