0

我只是想从头开始实现 customView,即通过扩展视图类并覆盖 onDraw() 方法。只是试图建立一个简单的视图,一个现在只画一个圆圈的视图。我在对齐它时遇到了一些问题,我无法理解 android 是如何计算视图尺寸的。只有视图即 setContentView(new MyCustomView(this)) 工作正常......它占用了整个空间并绘制了圆圈。但是,如果我施加任何限制,即给予边距,或在 centerparent 中对齐它会使我的视图完全丢失并且它不会绘制任何东西。问题是视图被其父级剪裁但无法理解为什么它被剪裁。对此的任何帮助将不胜感激。这是我的代码。

这是我的自定义视图

    public class MyCustomView extends View {

    private Paint myPaint=null;
    private boolean useCenters;
    private float xCoordinate;
    private float yCoordinate;
    private float viewWidth;
    private float viewHeight;
    private int totalTime;
    private static float SWEEP_INC ;
    private RectF myRect;
    private boolean shouldInvalidate;
    private float mSweep;

    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initPaintComponents();
    }

    public MyCustomView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyCustomView(Context context) {
        this(context,null);
    }

    private void initPaintComponents() {

        myPaint = new Paint();
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(4);
        myPaint.setColor(0x880000FF);
        useCenters = false;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        calculateCoordinates();
    }

    private void calculateCoordinates() {

        xCoordinate = getX();
        yCoordinate = getY();
        viewWidth = getWidth();
        viewHeight = getHeight();
        myRect = new RectF(xCoordinate+3, yCoordinate+3, xCoordinate+viewWidth-(viewWidth/10), yCoordinate+viewHeight-(viewHeight/10));
        Log.i("SAMPLEARC","xcoordinate: "+xCoordinate+" ycoordinate: "+yCoordinate+" view width:"+viewWidth+" view height:"+viewHeight+" measured width: "+getMeasuredWidth()+"measured height:"+getMeasuredHeight());
    }

    public int getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(int totalTime) {
        this.totalTime = totalTime;
        SWEEP_INC = (float)6/totalTime;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(myRect, 0, mSweep, useCenters, myPaint);

         mSweep += SWEEP_INC;
         if(mSweep > 280)
         {
             myPaint.setColor(0x888800FF);
         }

        invalidate();
    }
}

我的活动:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        MyCustomView myView = (MyCustomView) findViewById(R.id.customimg);
        myView.setTotalTime(10);
    }

主要的.xml

  RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
   com.example.anim.MyCustomView android:id="@+id/customimg"
  android:layout_width="100dp"
  android:layout_height="100dp"
  android:layout_centerInParent="true"

如果我在 xml 中删除该 centerInParent 它会被绘制。所以在 onMeasure() 中调用 setMeasureDimentions() 也没有任何影响。但是 xcoodinate、ycoordinate、viewWidth 和 viewHeight 似乎给出了正确的值。只需要了解视图被裁剪的原因以及 android 在运行时如何计算尺寸。以及在绘制这些自定义视图时如何考虑边距参数。任何帮助将不胜感激。

提前致谢

4

2 回答 2

0
<resources>
   <declare-styleable name="CustomAttrs">
    <attr name="xattr" format="string" />
</declare-styleable>
</resources>

在值/attrs.xml

于 2012-05-15T10:04:41.770 回答
0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:lib="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

  <com.ind.Custom_Attribute.LibView  
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content"    
  android:text=""
  lib:xattr="Custom attribute APPLIED!"
  />

</LinearLayout>     
于 2012-05-15T09:52:05.907 回答