国产探花免费观看_亚洲丰满少妇自慰呻吟_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;//    }}這樣呢 我們的自定義的小球就實現拖動效果了


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 恩施市| 六盘水市| 石嘴山市| 清原| 江安县| 民勤县| 湛江市| 珲春市| 紫金县| 南溪县| 禹城市| 获嘉县| 安达市| 司法| 定边县| 新田县| 广灵县| 宾阳县| 会宁县| 稻城县| 灵石县| 临汾市| 白沙| 西乌珠穆沁旗| 探索| 彩票| 大邑县| 合作市| 综艺| 临邑县| 阳信县| 仁寿县| 天门市| 招远市| 崇明县| 西华县| 大邑县| 剑川县| 西华县| 安图县| 永寿县|