android(3)自定义View

实现自定义View下面是代码:

package com.example.sy.fgb;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by sy on 2015/8/4.
 */
//自定义View需继承View
public class myView extends View{
    private float x=40;
    private float y=50;
    private float xx,yy;
    Paint paint=new Paint();
    //构造方法  为什么写3个,如果不写,后面会提示你缺少构造方法,无法在xml中使用
    public myView(Context context) {
        super(context);
    }
    // 需要实现onKeydown keyup ontouchenevt  ondeaw()

    public myView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public myView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return super.onKeyUp(keyCode, event);
    }
    //触摸监听。重画View实现动画
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        xx=event.getX();
        yy=event.getY();
        if(xx==x){
           if(yy>=y) y=y+10;
            else y=y-10;
        }
        else {
            float k = (yy - y) / (xx - x);
            float n = (float) (10 / (Math.sqrt((double) (k * k + 1))));
            float m = k * n;
            if (xx > x) {
                x = x + n;
                y = y + m;
            }
            if(xx<x){
                x = x - n;
                y = y - m;
            }
        }
        invalidate();
        return true;
    }
    //这里写视图view
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);

        paint.setColor(Color.RED);
       // canvas.drawText("hello world ",x,y,paint);
        canvas.drawCircle(x, y, 15, paint);
    }

}

在xml中使用自定义View

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    >



    <com.example.sy.fgb.myView
        android:id="@+id/hello"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="25dp"
        android:layout_marginEnd="25dp"
        android:layout_marginTop="22dp" />
</RelativeLayout>

Activity中加载页面

package com.example.sy.fgb;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;


public class game extends Activity {
    //自定义View的使用
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.game);
        //setContentView(new myView(this));  //也可以这样写
        TextView text= (TextView) findViewById(R.id.textView);
        text.setText("我的自定义页面");
    }

}

完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值