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

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

跟隨手指移動小球

2019-11-09 15:47:06
字體:
來源:轉載
供稿:網友
1、首先自定義UI組件

package com.example.customdemo;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;/** * Created by w10 on 2017/2/8. */public class DrawView extends View {    public float currentX=40;    public float currentY=50;    //定義并創建一個畫筆    Paint paint=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);        //設置畫筆顏色        paint.setColor(Color.RED);        canvas.drawCircle(currentX,currentY,15,paint);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        //修改兩個屬性        currentX=event.getX();        currentY=event.getY();        invalidate();        return true;    }}2、將封裝好的類作為一個控件在activity中使用package com.example.customdemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.LinearLayout;public class CostomActivity extends AppCompatActivity {    private LinearLayout costom;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_costom);        initView();    }    private void initView() {        costom= (LinearLayout) findViewById(R.id.costom);        final  DrawView draw= new DrawView(this);        draw.setMinimumWidth(300);        draw.setMinimumHeight(500);    }}

3、布局所用xml文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/costom"    android:layout_width="match_parent" android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.customdemo.CostomActivity">    <com.example.customdemo.DrawView        android:id="@+id/drawView"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新和县| 鸡西市| 财经| 安岳县| 池州市| 大理市| 平定县| 呼和浩特市| 江口县| 商河县| 岫岩| 桦甸市| 缙云县| 南澳县| 新巴尔虎左旗| 罗山县| 藁城市| 宝清县| 铜山县| 长沙市| 禹城市| 呼和浩特市| 旌德县| 密山市| 公主岭市| 玉田县| 铜川市| 获嘉县| 武川县| 永州市| 百色市| 原平市| 砀山县| 于都县| 洛宁县| 普定县| 射阳县| 文水县| 綦江县| 晋州市| 台东县|