0

I'm design any squares in custom layout extends ImageView but I can not find any document about how to set radius or round squares my squares is like with this:

enter image description here

but I need like with this:

enter image description here

My code:

public class GameView extends ImageView {
    private Paint paint;
    private Paint textPaint;
    private int with;
    private int height;
    private int[][] cells = new int[4][4];
    public GameView(Context context) {
        super(context);
        initialize();
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public GameView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    public void initialize(){
        paint = new Paint();
        paint.setColor(Color.parseColor("#afafaf"));
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setAntiAlias(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int size = with / 4;
        int padding = 5;
        for ( int i=0; i < 4 ; i++)
        {
            for( int j=0; j < 4 ; j++){
                Rect rect = new Rect( i*size + padding , j*size + padding , (i+1) *size - padding, (j+1)*size - padding );
                canvas.drawRect(rect, paint);
            }
        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

This is all you have to do. In your drawable folder create an xml file called rounded_rect.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 

    <solid android:color="#afafaf"/>    

    <stroke android:width="3dp"
        android:color="#ffafafaf"/>

    <padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 

    <corners android:radius="5px"/> 
</shape>

And then in the xml containing your ImageView, apply the attribute to your ImageView

android:src="@drawable/rounded_rect"

I haven't tested this though, if it doesn't work let me know and I will do some modifying.

zgc7009
  • 3,371
  • 5
  • 22
  • 34
  • OH no!! i want to set radius for `Rect` in this line: `Rect rect = new Rect( i*size + padding , j*size + padding , (i+1) *size - padding, (j+1)*size - padding );` not for xml layout such as `EditText` or etc. –  Oct 10 '14 at 17:30
  • @andbee I might be confused on what you are asking because this will set the radius for a rectangle, just use it and do what you want. Not quite sure why you are so set on using a custom view in Java for managing this but if that is what you really want to do I guess someone else may be able to help. – zgc7009 Oct 10 '14 at 17:33