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

Android自定義組件跟隨自己手指主動畫圓

 更新時間:2017年07月24日 14:55:08   作者:yangykaifa  
這篇文章主要為大家詳細介紹了Android自定義組件跟隨自己手指主動畫圓,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)跟隨手指畫圓的具體代碼,供大家參考,具體內(nèi)容如下

首先自己定義一個View子類:

package com.example.androidtest0.myView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class DrawView extends View {
 public float currentX = 40;
 public float currentY = 50;
 //定義、并創(chuàng)建畫筆
 Paint p = new Paint();
 public DrawView(Context context) {
 super(context);
 }

 public DrawView(Context context, AttributeSet attrs) {
 super(context, attrs);
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 //設置畫筆的顏色
 p.setColor(Color.RED);
 //繪制一個小球
 canvas.drawCircle(currentX, currentY, 15, p);
 }
 
 /**
 * 為該組件的觸碰事件重寫事件處理方法
 */
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 //改動currentX、currentY兩個屬性
 currentX = event.getX();
 currentY = event.getY();
 //通知當前組件重繪自己
 invalidate();
 return true;
 }
 
  
 

}

主界面XML:

custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/root"
  android:orientation="vertical" >

</LinearLayout>

主activity:

package com.example.androidtest0;

import com.example.androidtest0.myView.DrawView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

public class CustomView extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.custom_layout);
 //獲取布局文件里LinearLayout容器
 LinearLayout root = (LinearLayout)findViewById(R.id.root);
 //創(chuàng)建DrawView組件
 final DrawView drawView = new DrawView(this);
 //設置自己定義組件的最小寬度、高度
 drawView.setMinimumWidth(10);
 drawView.setMinimumHeight(10);
 root.addView(drawView);
 }
}


效果:


除此之外:

還能夠用XML的方式:也是首先建一個View的子類。和上面一樣。

然后主界面XML例如以下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/root"
  android:orientation="vertical" >

  <com.example.androidtest0.myView.DrawView 
    android:layout_width="match_parent" android:layout_height="match_parent"
    />
</LinearLayout>

主activity文件例如以下:

package com.example.androidtest0;

import com.example.androidtest0.myView.DrawView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

public class CustomView extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.custom_layout);
 }
}


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

相關文章

  • Android實現(xiàn)每天定時提醒功能

    Android實現(xiàn)每天定時提醒功能

    本文主要介紹了Android每天定時提醒功能、定時功能、鬧鐘的相關知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-04-04
  • Android開發(fā)筆記之:消息循環(huán)與Looper的詳解

    Android開發(fā)筆記之:消息循環(huán)與Looper的詳解

    本篇文章是對Android中消息循環(huán)與Looper的應用進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • Flutter網(wǎng)絡請求Dio庫的使用及封裝詳解

    Flutter網(wǎng)絡請求Dio庫的使用及封裝詳解

    本文主要介紹了Flutter網(wǎng)絡請求Dio庫的使用及封裝詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • Android通過多點觸控的方式對圖片進行縮放的實例代碼

    Android通過多點觸控的方式對圖片進行縮放的實例代碼

    這篇文章主要介紹了Android通過多點觸控的方式對圖片進行縮放的實例代碼,完成了點擊圖片就能瀏覽大圖的功能,并且在瀏覽大圖的時候還可以通過多點觸控的方式對圖片進行縮放。
    2018-05-05
  • android實現(xiàn)簡易計算器

    android實現(xiàn)簡易計算器

    這篇文章主要為大家詳細介紹了android實現(xiàn)簡易計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android ListView滾動到指定的位置

    Android ListView滾動到指定的位置

    這篇文章主要給大家介紹了Android中的ListView如何滾動到指定的位置,文章給出了兩種解決的方法,并給出詳細的示例代碼,相信會對大家的理解和學習很有幫助,有需要的朋友們下面來一起看看吧。
    2016-10-10
  • Android使用自定義ImageView實現(xiàn)圓形圖片效果

    Android使用自定義ImageView實現(xiàn)圓形圖片效果

    本篇文章主要介紹了Android使用自定義ImageView實現(xiàn)圓形圖片效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 淺析Android 的 MediaPlayer類

    淺析Android 的 MediaPlayer類

    本文主要介紹了Android的mediaplayer類作用和用法,并附上了關鍵代碼,有需要的朋友可以參考下
    2014-10-10
  • Android開發(fā)中button按鈕的使用及動態(tài)添加組件方法示例

    Android開發(fā)中button按鈕的使用及動態(tài)添加組件方法示例

    這篇文章主要介紹了Android開發(fā)中button按鈕的使用及動態(tài)添加組件方法,涉及Android針對button按鈕的事件響應及TextView動態(tài)添加相關操作技巧,需要的朋友可以參考下
    2017-11-11
  • Android實現(xiàn)簡易計算器功能

    Android實現(xiàn)簡易計算器功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)簡易計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06

最新評論