Android自定義組件跟隨自己手指主動(dòng)畫圓
本文實(shí)例為大家分享了Android實(shí)現(xiàn)跟隨手指畫圓的具體代碼,供大家參考,具體內(nèi)容如下
首先自己定義一個(gè)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);
//繪制一個(gè)小球
canvas.drawCircle(currentX, currentY, 15, p);
}
/**
* 為該組件的觸碰事件重寫事件處理方法
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
//改動(dòng)currentX、currentY兩個(gè)屬性
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的方式:也是首先建一個(gè)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);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 架構(gòu)之?dāng)?shù)據(jù)庫框架搭建
這篇文章主要給大家介紹的是Android 架構(gòu)之?dāng)?shù)據(jù)庫框架搭建,在本篇中,將會(huì)讓你一點(diǎn)一滴從無到有創(chuàng)建一個(gè)不再為數(shù)據(jù)庫而煩惱的框架。需要的朋友可以參考下面文章的具體內(nèi)容2021-09-09
Android實(shí)現(xiàn) Shape屬性gradient 漸變效果
這篇文章主要介紹了Android 實(shí)現(xiàn)Shape屬性gradient 漸變效果,gradient用以定義漸變色,可以定義兩色漸變和三色漸變,及漸變樣式,具體實(shí)現(xiàn)代碼感興趣的朋友跟隨小編一起看看吧2019-11-11
Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對(duì)話框的功能
本篇文章主要介紹了Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對(duì)話框的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04
Android動(dòng)態(tài)自定義圓形進(jìn)度條
這篇文章主要介紹了Android動(dòng)態(tài)自定義圓形進(jìn)度條,需要的朋友可以參考下2017-03-03
Android中使用二級(jí)緩存、異步加載批量加載圖片完整案例
這篇文章主要介紹了Android中使用二級(jí)緩存、異步加載批量加載圖片完整案例,本文講解了實(shí)現(xiàn)的過程以及核心代碼展示,并給出了完整項(xiàng)目源碼,需要的朋友可以參考下2015-06-06
Android AlertDialog六種創(chuàng)建方式案例詳解
這篇文章主要介紹了Android AlertDialog六種創(chuàng)建方式案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android 后臺(tái)發(fā)送郵件示例 (收集應(yīng)用異常信息+Demo代碼)
今天介紹個(gè)更簡(jiǎn)單的方法,我們把異常信息收集后,通過后臺(tái)發(fā)送郵件方法,把相關(guān)異常信息發(fā)送到我們指定的郵箱里面2013-07-07
Android之AnimationDrawable簡(jiǎn)單模擬動(dòng)態(tài)圖
這篇文章主要為大家詳細(xì)介紹了Android之AnimationDrawable簡(jiǎn)單模擬動(dòng)態(tài)圖的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

