开发者

custom View not displayed well in android

My custom view does not display entirely. Please see my screenshot:

custom View not displayed well in android

And the source code

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),开发者_StackOverflow
               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;


}


Making the assumption that you don't want the clipping you see in your screenshot. Your problem is that the values you return in onMeasure don't account for your x, y offsets:

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


What exactly do you want to Achieve? if you want to be fullscreen then use the FILL_PARENT flag instead of WRAP_CONTENT at least for the width of your view. also for the height there is a weight parameter that might help even the height of your view. but since its a custom drawing i cant help you further if there are any adjustments needed in your view code. you have to figure that out for yourself.


The radius of each of your "dots" is identical, and this directly translates into the answer you return in onMeasure(). You're changing the x and y location of the center, getting further from the actual View canvas.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜