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

Android自定義組件跟隨自己手指主動(dòng)畫圓

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

本文實(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)文章

最新評(píng)論

赞皇县| 内黄县| 五原县| 阿拉善盟| 福建省| 朔州市| 井陉县| 南阳市| 淮滨县| 潜江市| 杭锦旗| 灵川县| 金塔县| 廉江市| 准格尔旗| 营山县| 固原市| 金华市| 贺兰县| 子洲县| 昌黎县| 仁布县| 黑龙江省| 桐柏县| 宝丰县| 鸡泽县| 寻甸| 平乐县| 宁国市| 金湖县| 鄂托克前旗| 黎平县| 汝州市| 林口县| 富蕴县| 商南县| 澄迈县| 防城港市| 台中县| 藁城市| 彭泽县|