Android自定義組件跟隨自己手指主動畫圓
本文實(shí)例為大家分享了Android實(shí)現(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);
//設(shè)置畫筆的顏色
p.setColor(Color.RED);
//繪制一個小球
canvas.drawCircle(currentX, currentY, 15, p);
}
/**
* 為該組件的觸碰事件重寫事件處理方法
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
//改動currentX、currentY兩個屬性
currentX = event.getX();
currentY = event.getY();
//通知當(dāng)前組件重繪自己
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);
//獲取布局文件里L(fēng)inearLayout容器
LinearLayout root = (LinearLayout)findViewById(R.id.root);
//創(chuàng)建DrawView組件
final DrawView drawView = new DrawView(this);
//設(shè)置自己定義組件的最小寬度、高度
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)筆記之:消息循環(huán)與Looper的詳解
本篇文章是對Android中消息循環(huán)與Looper的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Flutter網(wǎng)絡(luò)請求Dio庫的使用及封裝詳解
本文主要介紹了Flutter網(wǎng)絡(luò)請求Dio庫的使用及封裝詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Android通過多點(diǎn)觸控的方式對圖片進(jìn)行縮放的實(shí)例代碼
這篇文章主要介紹了Android通過多點(diǎn)觸控的方式對圖片進(jìn)行縮放的實(shí)例代碼,完成了點(diǎn)擊圖片就能瀏覽大圖的功能,并且在瀏覽大圖的時候還可以通過多點(diǎn)觸控的方式對圖片進(jìn)行縮放。2018-05-05
Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果
本篇文章主要介紹了Android使用自定義ImageView實(shí)現(xiàn)圓形圖片效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Android開發(fā)中button按鈕的使用及動態(tài)添加組件方法示例
這篇文章主要介紹了Android開發(fā)中button按鈕的使用及動態(tài)添加組件方法,涉及Android針對button按鈕的事件響應(yīng)及TextView動態(tài)添加相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Android實(shí)現(xiàn)簡易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06

