zoukankan      html  css  js  c++  java
  • 自定义View组件

      

      自定义组件是通过继承View类,重写View类的方法而得,得到个性化的颜色、呈现、行为的组件。


    一个圆:

    public class SelfView extends View {
        float currentX=40;
        float currentY=50;
      //坐标,为float类型。 Paint paint
    =new Paint();
      //画笔,
    public SelfView(Context context) { super(context); } public SelfView(Context context,AttributeSet set){ super(context,set); }
      //以上是SelfView类的两个构造函数
      //重写了View类的onDraw()方法,其参数是Canvas类型的 @Override
    public void onDraw(Canvas canvas){ super.onDraw(canvas);
      //指定画笔颜色 paint.setColor(Color.BLUE);
      //绘画图形 canvas.drawCircle(currentX,currentY,
    25,paint); }
      //重写了onTouchEvent事件 @Override
    public boolean onTouchEvent(MotionEvent motionEvent){
      //获取触摸下的新坐标位置 currentX
    =motionEvent.getX(); currentY=motionEvent.getY();
      //通知当前组件重绘自己,为什么不能通过this.onDraw(),或onDraw()呢? invalidate();
    return true; } }

    像添加系统组件那样添加自定义的SelfView组件:

    SelfView selfView=new SelfView(this);
            root.addView(selfView);

    也可以在XML中:

    <com.×××.×××.×××.SelfView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    
    
  • 相关阅读:
    计蒜客 移除数组中的重复元素 (双指针扫描)
    计蒜客 寻找插入位置 (二分查找)
    poj 1007 DNA Sorting
    全排列函数 nyoj 366(next_permutation()函数)
    nyoj 202 红黑树
    nyoj 92 图像有用区域
    nyoj 82 迷宫寻宝(一)
    nyoj 58 最少步数
    nyoj 43 24 Point game
    nyoj 42 一笔画问题
  • 原文地址:https://www.cnblogs.com/zhangyanxiang/p/4827674.html
Copyright © 2011-2022 走看看