2012-05-15 71 views
0

我只是试图从头开始实现自定义视图,即通过扩展视图类并重写onDraw()方法。试图建立一个简单的视图,现在只是画一个圆圈的视图。我在对齐时遇到了一些问题,我无法理解android如何计算视图尺寸。 只有视图,即setContentView(新的MyCustomView(this))可以正常工作......它占用整个空间并绘制圆。但是,如果我施加任何约束条件,即提供保证金,或者将其调整为中等水平,则会使我的观点完全丧失,并且不会绘制任何内容。问题是该视图被其父母截断,但无法理解为什么它被截断。任何有关这方面的帮助将不胜感激。这是我的代码。对齐自定义视图的问题

这里是我的customView

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(); 
    } 
} 

MyActivity:

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

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

main.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" 

如果我删除centerInParent在XML中它被抽出。所以在onMeasure()中调用setMeasureDimentions()也没有任何影响。但是,xcoodinate,ycoordinate,viewWidth和viewHeight似乎给出了正确的值。只需要理解为什么视图被剪裁,android如何在运行时计算尺寸。在绘制这些customView时,我如何考虑margin参数。任何帮助将不胜感激。

在此先感谢

+0

看到这一点,就会帮你解决你的问题... HTTP://stackoverflow.com/questions/9205838 /如何绘制圈与分区在Android – himanshu

+0

嗨Himanshu感谢您的帮助....但我其实并没有试图画一个静态的圈子....我palnning设计我自己的customview这将类似于Android中的任何视图(textview,edittext),这是一个可以在任何应用程序中使用的可重用视图,并且可以包含在任何xm湖在我的视图的onMeasure()中是否有任何缺失?在计算我的rectF的尺寸时,是否必须考虑其他参数? – user1395403

回答

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>  
+0

嗨Shivang,我无法完全理解你的意思。你能告诉我你到底想要改变什么吗? – user1395403

+0

xmlns:lib =“http://schemas.android.com/apk/res-auto” – sss

+0

此行用于自定义属性 – sss

0
<resources> 
    <declare-styleable name="CustomAttrs"> 
    <attr name="xattr" format="string" /> 
</declare-styleable> 
</resources> 
中值/ attrs.xml

+0

这是为了向定制组件添加额外的参数。但那不是我的情况...我希望现有的参数(边距,中心无效)与我的自定义视图一起工作。感谢您的帮助。 – user1395403

+0

在我的视图的onMeasure()中是否有任何缺少的东西?在计算我的rectF的尺寸时,是否必须考虑其他参数? – user1395403