在自定义视图中添加圆角

时间:2013-05-28 11:56:24

标签: android drawing drawable rounded-corners

我对Android很新,我正在尝试使用自定义视图(使用画布)。我有一些线条&在那里。关键是,我现在想要给整个视图圆角,但这不能很好地工作,因为我正在绘制视图,我的绘图在圆角上方,这是通过资源添加的。是否有可能添加圆角,覆盖整个视图?

致以最诚挚的问候,感谢您的帮助!

2 个答案:

答案 0 :(得分:4)

我不确定你的要求。但您可以使用以下内容并修改相同的

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl"
android:background="@drawable/bkg" //set background
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
</RelativeLayout>

bkg.xml

 <?xml version="1.0" encoding="UTF-8"?> 
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle"> 
 <solid android:color="#10EB0A"/>    //set color
 <stroke android:width="3dp"         // set border  
 android:color="#0FECFF" />          //set border color 
 <padding android:left="5dp"         //set padding
 android:top="5dp" 
 android:right="5dp"
 android:bottom="5dp"/> 
 <corners android:bottomRightRadius="20dp" //set radius
 android:bottomLeftRadius="20dp" 
 android:topLeftRadius="20dp"
 android:topRightRadius="20dp"/> 
 </shape> 

MainActivity.java

    public class MainActivity extends Activity {

RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rl = (RelativeLayout) findViewById(R.id.rl);
    CustomView cv = new CustomView(this);
    rl.addView(cv);  //add custom view to the relative layout
} 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
class CustomView extends View
{
    Bitmap bmp;
    PaintDrawable mDrawable;

    public CustomView(Context context) {
        super(context); 
        bmp = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        canvas.drawBitmap(bmp, 200, 200, null);
    }   
       }
  }

快照

enter image description here

答案 1 :(得分:3)

    public class RoundCornerView extends View{
    public RoundCornerView(Context context) {
        super(context);
    }

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

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

    protected void onDraw(android.graphics.Canvas canvas)
    {
        Paint paint = new Paint();

        paint.setAlpha(255);
        canvas.translate(0, 30);
        paint.setColor(Color.BLUE);
        Path mPath = new Path();
        mPath.addRoundRect(new RectF(0, 0, 100,100),20,20, Path.Direction.CCW);
        canvas.clipPath(mPath, Region.Op.INTERSECT);
        paint.setColor(Color.GREEN);
        paint.setAntiAlias(true);
        canvas.drawRect(0, 0, 120,120,paint);

    }
}

尝试使用clippath,但就像3.0后的旁注一样,你需要在显示中关闭hardwareAccelerated

  

机器人:硬件加速= “假”

有解决方案,成立时会将其作为补充发布在