实现自定义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("我的自定义页面");
}
}
完成。