自定义View的使用
来源:互联网 发布:大阪环球影城 知乎 编辑:程序博客网 时间:2024/06/10 19:38
第一种:通过java代码实现自定义view添加到显示布局中
第一步:定义布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylinearlayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</LinearLayout>
第二步:定义自定义View
public class MyView extends View {
private float current_x=20;
public float getCurrent_x() {
return current_x;
}
public void setCurrent_x(float currentX) {
current_x = currentX;
}
public float getCurrent_y() {
return current_y;
}
public void setCurrent_y(float currentY) {
current_y = currentY;
}
private float current_y=20;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p=new Paint();
p.setColor(Color.RED);
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.shoushi);
canvas.drawBitmap(bitmap, this.getCurrent_x(), this.getCurrent_y(), p);
}
}
第三步:测试类实现
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myview_main);
LinearLayout ll=(LinearLayout)findViewById(R.id.mylinearlayout);
final MyView mv=new MyView(this, null);
mv.setBackgroundColor(Color.BLUE);
mv.setLayoutParams(new LayoutParams(300, 300));
mv.setMinimumWidth(300);
mv.setMinimumHeight(300);
ll.addView(mv);
mv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mv.setCurrent_x(event.getX());
mv.setCurrent_y(event.getY());
mv.invalidate();
return false;
}
});
}
第四步:演示效果
第二种方式:通过在布局文件中添加标签
第一步:布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylinearlayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<sun.zone.android.test.MyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</sun.zone.android.test.MyView>
</LinearLayout>
第二步:自定义View
public class MyView extends View {
private float current_x=20;
public float getCurrent_x() {
return current_x;
}
public void setCurrent_x(float currentX) {
current_x = currentX;
}
public float getCurrent_y() {
return current_y;
}
public void setCurrent_y(float currentY) {
current_y = currentY;
}
private float current_y=20;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p=new Paint();
p.setColor(Color.RED);
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.shoushi);
canvas.drawBitmap(bitmap, this.getCurrent_x(), this.getCurrent_y(), p);
}
}
第三步:测试类
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myview_main);
final MyView mv=(MyView)findViewById(R.id.myview1);
mv.setBackgroundColor(Color.BLUE);
mv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mv.setCurrent_x(event.getX());
mv.setCurrent_y(event.getY());
mv.invalidate();
return false;
}
});}
第四步:演示效果
- 自定义View的使用
- 自定义view的使用
- Android 自定义View的使用
- 自定义view的简单使用
- 自定义View的使用(一)
- Android自定义View的使用
- 使用自定义图形的 view
- android!自定义view的使用
- 自定义View属性的使用
- Android-自定义View的使用
- 自定义控件View的使用
- 自定义View的attr文件的使用
- 自定义View和Matrix的使用
- Android中自定义View的MeasureSpec使用
- Android中自定义View的MeasureSpec使用
- Android之自定义View的使用
- 自定义View Xml中的使用的注意事项
- Android中自定义View的MeasureSpec使用
- 根据不同环境加载不同js文件
- RHEL6 网络配置
- 【计算几何】凸多边形
- python安装软件 No module named setuptools
- GridView 合并单元格的方法
- 自定义View的使用
- Struts2的学习笔记(3)
- 浏览器中的“Linux”
- 扩展方法的使用
- 详解css定位与定位应用
- Perl使用DBI模块以SYSDBA角色连接数据库
- LINUX共享内存使用常见陷阱与分析
- Oracle操作符
- mysql-python安装 的问题