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

Android自定義view圓并隨手指移動

 更新時間:2017年08月28日 11:51:31   作者:heibuke  
這篇文章主要為大家詳細(xì)介紹了Android自定義view圓并隨手指移動,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android自定義view圓并隨手指移動的具體代碼,供大家參考,具體內(nèi)容如下

main代碼

public class MainActivity extends AppCompatActivity {
 private int screenW; //屏幕寬度
 private int screenH; //屏幕高度
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 Display dis = this.getWindowManager().getDefaultDisplay();
 // 設(shè)置全屏
 requestWindowFeature(Window.FEATURE_NO_TITLE);
 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  WindowManager.LayoutParams.FLAG_FULLSCREEN);
 // 獲取屏幕寬度
 screenW = dis.getWidth();
 // 獲取屏幕高度
 screenH = dis.getHeight();
 setContentView(new MyView(this));
 }
 //自定義繪圖類
 class MyView extends View {
 private Paint paint; //定義畫筆
 private float cx = 50; //圓點(diǎn)默認(rèn)X坐標(biāo)
 private float cy = 50; //圓點(diǎn)默認(rèn)Y坐標(biāo)
 private int radius = 20;
 //定義顏色數(shù)組
 private int colorArray[] = {Color.BLACK,Color.BLACK,Color.GREEN,Color.YELLOW, Color.RED};
 private int paintColor = colorArray[0]; //定義畫筆默認(rèn)顏色

 public MyView(Context context) {
  super(context);
  //初始化畫筆
  initPaint();
 }
 private void initPaint(){
  paint = new Paint();
  //設(shè)置消除鋸齒
  paint.setAntiAlias(true);
  //設(shè)置畫筆顏色
  paint.setColor(paintColor);
 }

 //重寫onDraw方法實(shí)現(xiàn)繪圖操作
 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  //將屏幕設(shè)置為白色
  canvas.drawColor(Color.WHITE);
  //修正圓點(diǎn)坐標(biāo)
  revise();
  //隨機(jī)設(shè)置畫筆顏色
  setPaintRandomColor();
  //繪制小圓作為小球
  canvas.drawCircle(cx, cy, radius, paint);
 }

 //為畫筆設(shè)置隨機(jī)顏色
 private void setPaintRandomColor(){
  Random rand = new Random();
  int randomIndex = rand.nextInt(colorArray.length);
  paint.setColor(colorArray[randomIndex]);
 }

 //修正圓點(diǎn)坐標(biāo)
 private void revise(){
  if(cx <= radius){
  cx = radius;
  }else if(cx >= (screenW-radius)){
  cx = screenW-radius;
  }
  if(cy <= radius){
  cy = radius;
  }else if(cy >= (screenH-radius)){
  cy = screenH-radius;
  }
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN:
   // 按下
   cx = (int) event.getX();
   cy = (int) event.getY();
   // 通知重繪
   postInvalidate(); //該方法會調(diào)用onDraw方法,重新繪圖
   break;
  case MotionEvent.ACTION_MOVE:
   // 移動
   cx = (int) event.getX();
   cy = (int) event.getY();
   // 通知重繪
   postInvalidate();
   break;
  case MotionEvent.ACTION_UP:
   // 抬起
   cx = (int) event.getX();
   cy = (int) event.getY();
   // 通知重繪
   postInvalidate();
   break;
  }

  /*
  * 備注1:此處一定要將return super.onTouchEvent(event)修改為return true,原因是:
  * 1)父類的onTouchEvent(event)方法可能沒有做任何處理,但是返回了false。
  * 2)一旦返回false,在該方法中再也不會收到MotionEvent.ACTION_MOVE及MotionEvent.ACTION_UP事件。
  */
  //return super.onTouchEvent(event);
  return true;
 } }


}

布局

<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.sn.MainActivity">
 <com.example.sn.MainActivity.MyView
 android:id="@+id/myview"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 />
</RelativeLayout>

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

相關(guān)文章

  • Android seekbar實(shí)現(xiàn)可拖動進(jìn)度條

    Android seekbar實(shí)現(xiàn)可拖動進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android seekbar實(shí)現(xiàn)可拖動進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • android自定義滾動上下回彈scollView

    android自定義滾動上下回彈scollView

    這篇文章主要為大家詳細(xì)介紹了android自定義滾動上下回彈scollView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android中正確使用字體圖標(biāo)(iconfont)的方法

    Android中正確使用字體圖標(biāo)(iconfont)的方法

    IconFont字體不僅僅流行于Web開發(fā),在移動開發(fā)中也漸漸的使用的范圍更廣泛。這篇文章主要介紹了在Android開發(fā)中使用icon font的代碼和方法。對大家學(xué)習(xí)使用iconfont有一定的參考借鑒價值,有需要的朋友們下面來一起看看吧。
    2016-10-10
  • Android實(shí)現(xiàn)波浪線效果(xml bitmap)

    Android實(shí)現(xiàn)波浪線效果(xml bitmap)

    這篇文章主要介紹了Android xml bitmap實(shí)現(xiàn)波浪線效果,制作過程簡單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-01-01
  • 基于Flutter實(shí)現(xiàn)短信驗(yàn)證碼監(jiān)控與轉(zhuǎn)發(fā)

    基于Flutter實(shí)現(xiàn)短信驗(yàn)證碼監(jiān)控與轉(zhuǎn)發(fā)

    這篇文章主要為大家詳細(xì)介紹了如何基于Flutter實(shí)現(xiàn)短信驗(yàn)證碼監(jiān)控與轉(zhuǎn)發(fā)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03
  • Android長按imageview把圖片保存到本地的實(shí)例代碼

    Android長按imageview把圖片保存到本地的實(shí)例代碼

    本文通過代碼給大家介紹了Android長按imageview把圖片保存到本地的實(shí)現(xiàn)方法,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2017-12-12
  • Flutter?Zone異常處理方法及基本原理

    Flutter?Zone異常處理方法及基本原理

    這篇文章主要為大家介紹了Flutter?Zone異常處理方法及基本原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Android實(shí)現(xiàn)Service獲取當(dāng)前位置(GPS+基站)的方法

    Android實(shí)現(xiàn)Service獲取當(dāng)前位置(GPS+基站)的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)Service獲取當(dāng)前位置(GPS+基站)的方法,較為詳細(xì)的分析了Service基于GPS位置的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • Android小掛件(APP Widgets)設(shè)計指導(dǎo)

    Android小掛件(APP Widgets)設(shè)計指導(dǎo)

    這篇文章主要為大家詳細(xì)介紹了Android小掛件APP Widgets設(shè)計指導(dǎo),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • SafeList?in?Flutter?and?Dart小技巧

    SafeList?in?Flutter?and?Dart小技巧

    這篇文章主要為大家介紹了SafeList?in?Flutter?and?Dart小技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12

最新評論

侯马市| 临泽县| 蓬溪县| 旅游| 蕲春县| 姚安县| 平遥县| 秭归县| 肇东市| 马尔康县| 万全县| 鹤岗市| 漳浦县| 平乐县| 陇西县| 马关县| 滨州市| 沧源| 宜宾市| 镶黄旗| 三原县| 城口县| 涿鹿县| 忻城县| 泰和县| 汉寿县| 大悟县| 永宁县| 嘉荫县| 翁牛特旗| 荣昌县| 邹平县| 淳安县| 延寿县| 通许县| 灵武市| 罗城| 沅江市| 武川县| 通榆县| 铅山县|