Android如何實(shí)現(xiàn)自定義組件跟隨自己手指主動(dòng)畫圓

小編給大家分享一下Android如何實(shí)現(xiàn)自定義組件跟隨自己手指主動(dòng)畫圓,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

公司主營(yíng)業(yè)務(wù):成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)推出疏附免費(fèi)做網(wǎng)站回饋大家。

具體內(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);
 }
}

效果:

Android如何實(shí)現(xiàn)自定義組件跟隨自己手指主動(dòng)畫圓

除此之外:

還能夠用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);
 }
}

看完了這篇文章,相信你對(duì)“Android如何實(shí)現(xiàn)自定義組件跟隨自己手指主動(dòng)畫圓”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

本文標(biāo)題:Android如何實(shí)現(xiàn)自定義組件跟隨自己手指主動(dòng)畫圓
分享網(wǎng)址:http://muchs.cn/article22/iiopjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、網(wǎng)頁(yè)設(shè)計(jì)公司標(biāo)簽優(yōu)化、網(wǎng)站制作品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站內(nèi)鏈

廣告

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

h5響應(yīng)式網(wǎng)站建設(shè)