我对Android非常陌生,我试图在自定义视图上绘制(使用画布)。我有一些& rects那里。关键是,我现在想给整个视图带来圆角,但是这样做效果不好,因为我正在绘制视图,而且我的绘图位于通过资源添加的圆角之上。是否有可能添加圆角,覆盖整个视图?将圆角添加到自定义视图
最好的问候,并感谢任何帮助!
我对Android非常陌生,我试图在自定义视图上绘制(使用画布)。我有一些& rects那里。关键是,我现在想给整个视图带来圆角,但是这样做效果不好,因为我正在绘制视图,而且我的绘图位于通过资源添加的圆角之上。是否有可能添加圆角,覆盖整个视图?将圆角添加到自定义视图
最好的问候,并感谢任何帮助!
我不确定你的要求。但是你可以使用下面的和修改相同
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);
}
}
}
快照
嗨,这不能正常工作,因为这不起作用与图纸。不过谢谢你的回答! –
@LucèBrùlè它与绘图一起工作。我在画布上绘制了一个位图图像。你能看到屏幕上的ic_launcher.png吗? canvas.drawBitmap(bmp,200,200,null);你可以绘制线条或路径 – Raghunandan
我画了很多行和recs,填充整个视图,然后它不起作用,因为我的绘图在圆角以上:( –
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后,您需要关闭硬件加速在你的清单
安卓硬件加速=“假”
有解决方案,当成立时将张贴在这里作为补充
这是图像与圆角。 OP是要求带圆角的自定义视图 – Raghunandan
事实上,我的糟糕之处在于,在文章的评论中还有一些关于布局中圆角的讨论。而Imageview是View的一个子类 – QVDev
我建议你可以在xml文件中定义你的线性布局或相对位置。使用形状(圆角)设置背景。将内容设置为活动。初始化视图。创建自定义视图。将自定义视图添加到布局。在评论相关布局 – Raghunandan
是你自定义布局是布局文件吗?包含任何容器假设线性布局或其他 – UnderGround
我的自定义视图从视图延伸,只是一个视图,我在其上绘制东西。我的做法是给它圆角。 –
但是,这种观点是膨胀任何布局文件或不,因为我不熟悉的观点,为什么问或为什么我们可以设置背景视图? – UnderGround