自定义视图在android中显示不佳

时间:2011-06-16 18:02:25

标签: android view custom-view

我的自定义视图无法完全显示。请看我的截图:

enter image description here

源代码

package com.dots;
import android.graphics.Color;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Dots1Activity extends Activity
{
    private static final String TAG = "DotsActivity";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);          


        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        CustomDrawableView view1 = new CustomDrawableView(this, 50, 50, Constants.DOTS_RADIUS, Constants.DOTS_COLOR);
        CustomDrawableView view2 = new CustomDrawableView(this, 150, 150, Constants.DOTS_RADIUS, Constants.DOTS_COLOR);
        CustomDrawableView view3 = new CustomDrawableView(this, 300, 300, Constants.DOTS_RADIUS, Constants.DOTS_COLOR);
        ll.addView(view1, layoutParams);
        ll.addView(view2, layoutParams);
        ll.addView(view3, layoutParams);
        setContentView(ll);
    }

}

class CustomDrawableView extends View implements View.OnClickListener{
    private Context context;
   private int x, y, radius, color;
   public CustomDrawableView(Context context, int x, int y, int radius, int color) {
       super(context);
       this.context = context;
       this.x = x;
       this.y =y;
       this.radius = radius;
       this.color = color;
       setOnClickListener(this);
   }

   protected void onDraw(Canvas canvas) 
   {
       super.onDraw(canvas);
       Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
       paint.setColor(color);
       canvas.drawCircle(x, y, radius, paint);
   }

   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
       super.onMeasure(View.MeasureSpec.makeMeasureSpec(Constants.DOTS_RADIUS*2, View.MeasureSpec.EXACTLY),
               View.MeasureSpec.makeMeasureSpec(Constants.DOTS_RADIUS*2, View.MeasureSpec.EXACTLY));
   }

   public void onClick(View v) {
       Toast.makeText(this.context, 
               x+"-"+y+"-"+radius, 
               Toast.LENGTH_SHORT).show();  
   }
}



public interface Constants 
{
    public static final int DOTS_RADIUS = 50;
    public static final int DOTS_COLOR = Color.GREEN;
    public static final int NUM_DOTS_ROWS = 5;
    public static final int NUM_DOTS_COLS = 5;
    public static final int WIDTH_BETWEEN_DOTS = 100;
    public static final int HEIGHT_BETWEEN_DOTS = 100;


}

3 个答案:

答案 0 :(得分:0)

您想要达到什么目标?如果您想要全屏,那么至少对于视图的宽度使用FILL_PARENT标志而不是WRAP_CONTENT。同样对于高度,有一个重量参数可能有助于你的视图高度。但由于它是一个自定义绘图,如果您的视图代码中需要进行任何调整,我无法进一步帮助您。你必须自己解决这个问题。

答案 1 :(得分:0)

假设您不希望在屏幕截图中看到剪辑。您的问题是,您在onMeasure中返回的值不会考虑您的x,y偏移量:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    super.onMeasure(View.MeasureSpec.makeMeasureSpec(Constants.DOTS_RADIUS*2 + x, View.MeasureSpec.EXACTLY),
           View.MeasureSpec.makeMeasureSpec(Constants.DOTS_RADIUS*2 + y, View.MeasureSpec.EXACTLY));
}

答案 2 :(得分:0)

每个“点”的半径是相同的,这直接转换为你在onMeasure()中返回的答案。您正在更改中心的x和y位置,从实际的View画布中获得更多。