国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

自定義拖動小球

2019-11-09 14:58:16
字體:
來源:轉載
供稿:網友

今天我們來寫一個自定義小球 實現拖動效果

首先我們來看下布局文件怎么寫 下面這個呢就是mainActivity的布局文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.customviewi.customview.MainActivity">    <com.customviewi.customview.DrawView        android:id="@+id/qiu"        android:layout_width="match_parent"        android:layout_height="match_parent"         /></RelativeLayout>下面來看一下我們自己寫的類中的代碼

package com.customviewi.customview;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.util.FloatMath;import android.view.MotionEvent;import android.view.View;/** *  */public class DrawView extends View {    //成員變量    PRivate int width;    private int height;    private int x;    private int y;    private Boolean onBall;    public DrawView(Context context) {        this(context,null);    }    public DrawView(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    /**     * 測量方法     */    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        //得到小球的寬高,默認占滿全屏幕        width=this.getWidth();        height=this.getHeight();        //設置小球的位置        x=width/2;        y=height/2;    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //創建畫筆        Paint paint=new Paint();        //設置畫筆的顏色        paint.setColor(Color.RED);        // 畫出小球,前倆參數是代表圓在屏幕上的xy坐標,20是半徑        canvas.drawCircle(x,y,20,paint);    }    /**     * 觸摸事件     */    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()){            case MotionEvent.ACTION_DOWN:                //得到當前的坐標點                float x2=event.getX();                float y2=event.getY();                //判斷鼠標是不是在球 ,如果不用這個方法的話就會出現,點擊屏幕的任何位置小球就會過去//                onBall=isonBall(x2,y2);                break;            case MotionEvent.ACTION_MOVE:                x= (int) event.getX();                y= (int) event.getY();                //這個方法可以從新調用onDraw                postInvalidate();                break;            case MotionEvent.ACTION_UP:                break;        }        return true;    }//    public boolean isonBall(float x2,float y2) {////        float sqrt = FloatMath.sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y));//        float sqrt=FloatMath.//        return onBall;//    }}這樣呢 我們的自定義的小球就實現拖動效果了


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 扎兰屯市| 萨迦县| 荥经县| 普兰店市| 佛山市| 历史| 南木林县| 梨树县| 宣恩县| 广河县| 炎陵县| 琼结县| 许昌县| 安多县| 藁城市| 闽侯县| 孝感市| 清苑县| 盐池县| 平和县| 巩留县| 且末县| 景洪市| 洛浦县| 汶川县| 仪征市| 珠海市| 正定县| 万州区| 尚志市| 沂源县| 金川县| 营山县| 柞水县| 玛多县| 会泽县| 北宁市| 射洪县| 安义县| 石阡县| 林西县|