频道栏目
IT货架 > > 正文
自定义View控件
网友分享于:Jan 1, 1970 8:00:00 AM    来源: IT货架   
DrawView.java /* * 自定义控件 * */package com.frank.control;import android.content.Context;import android.graphics.*;import android.util.AttributeSet;import android.view.*;public class DrawView extends View { public float currentX = 40; public float currentY = 50; //定义、创建画笔 Paint p = new Paint(); public DrawView(Context context) { super(context); } public DrawView(Context context,AttributeSet set) { super(context,set); } @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); //设置画笔颜色 p.setColor(Color.RED); //绘制一个小圆 canvas.drawCircle(currentX,currentY,15,p); } //为该组件的触碰事件重写事件处理方法 @Override public boolean onTouchEvent(MotionEvent event) { currentX = event.getX(); currentY = event.getY(); //通知当前组件重绘自己 super.invalidate(); //返回true表明该处理方法已经处理该事件 return true; }} activity_main.xml    MainActivity.java package com.frank.userdfinedcontrol;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.*;//引用自己定义控件包名import com.frank.control.*;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RelativeLayout rl = (RelativeLayout)super.findViewById(R.id.root); final DrawView draw = new DrawView(this); draw.setMinimumWidth(300); draw.setMinimumHeight(500); rl.addView(draw); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }   
相关板块推荐

广告服务联系QQ:1134687142 | 网站地图

版权所有: IT货架- 内容来自互联网,仅供用于技术学习,请遵循相关法律法规. 京ICP备11030978号-1