Java Android画布:创建RoundRectShape对象
我已经为矩形形状创建了一个类,因为它是我的应用程序中的一个对象。然而,现在我希望角落是圆的。下面您可以看到它是一个裸体类,可以创建任意多个具有相同属性的矩形Java Android画布:创建RoundRectShape对象,java,android,android-canvas,shapedrawable,android-shapedrawable,Java,Android,Android Canvas,Shapedrawable,Android Shapedrawable,我已经为矩形形状创建了一个类,因为它是我的应用程序中的一个对象。然而,现在我希望角落是圆的。下面您可以看到它是一个裸体类,可以创建任意多个具有相同属性的矩形 public class customDrawable extends ShapeDrawable { public void setCustomDrawableAttributes(ShapeDrawable shapeDrawable){ int x = 0; int y = 0; i
public class customDrawable extends ShapeDrawable {
public void setCustomDrawableAttributes(ShapeDrawable shapeDrawable){
int x = 0;
int y = 0;
int width = 100;
int height = 100;
shapeDrawable.setBounds(x, y-height, x + width,y+height );
}
public ShapeDrawable createShape(){
return new ShapeDrawable(new RectShape());
}
}
更新:没有这个方法,我什么都不会画,因为没有大小。它只绘制通常的矩形。(更改为不显示特定于应用程序的方法的整数值)
}
从我的研究中,我发现我不能简单地添加圆角,而是必须创建一个圆形矩形形状的可移动。我用这个RoundRectShape
创建圆角矩形的每次尝试都失败了。不知何故,这个形状最终总是一个没有圆角的规则矩形
我正在寻找一个创建可绘制的roundRectShape的just bare bones类(如提供的那个)。高度和宽度并不重要,只要它有圆角。必须使用Java而不是XML
我尝试过创建圆形矩形的链接:
一,
二,
三,
四,
五,
六,
七,
八,。
9.为什么不使用Canvas
类的drawRoundRect
功能?
public class RoundRect{
整数l,r,t,b,rx,ry;
油漆
public RoundRect(int l,int r,int t,int b,int rx,int ry,Paint paint){
this.l=l;
this.r=r;
this.t=t;
this.b=b;
this.paint=paint;
}
public void draw(Canvas c,Paint paint){
c.drawRoundRect(l,t,r,b,rx,ry,paint);
}
}`
我创建了一个类MyRect
,用于为您绘制圆角Rect
public class MyRect {
public static Paint paint; // default paint use for all my MyRect objects
static {
paint=new Paint();
paint.setColor(Color.RED);
}
public float x,y,width,height;
public float roundStrength=30;
public MyRect(float x, float y, float width,float height){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public MyRect(float x, float y, float width,float height,float roundStrength){
this(x,y,width,height);
this.roundStrength=roundStrength;
}
public void draw(Canvas canvas){
canvas.drawRoundRect(x-width/2,y-height/2,x+width/2,y+height/2,roundStrength,roundStrength,paint);
}
}
创建上面的MyRect
对象是不够的,我们需要在任何容器中保留对象的引用,以便将来可以修改或删除该对象
static ArrayList<MyRect> myRects=new ArrayList<>();
完成后,创建对象并添加到容器
myRects.add(new MyRect(touchx,touchy,100,100));
或
您还可以扩展MyRect,如根据需要添加更多构造函数、方法和数据成员。自定义绘图
可以通过扩展drawable类来创建自定义drawable
创建自定义可绘制图形的步骤
1.子类Drawable并实现以下方法
public void draw(@NonNull Canvas Canvas)
-您将获得一个画布对象来绘制形状。在此处调用getBounds()方法以根据应用可绘制图形的视图获取尺寸
public void setAlpha(@IntRange(from=0,to=255)int alpha)
您将在此处获得一个alpha整数值,将其设置为绘制形状的主绘制
public void setColorFilter(@Nullable ColorFilter ColorFilter)
-您将在此处获得ColorFilter对象,将其设置为绘制形状的主绘制
public int getOpacity()
-在此处返回不透明度值,如PixelFormat.TRANSPARENT、PixelFormat.TRANSPARENT、PixelFormat.RGB_888等
2.在onDraw()
中调用canvas.drawRoundRect()方法来绘制形状
public void drawRoundRect(@android.support.annotation.NonNull android.graphics.RectF rect,float rx,float ry,@android.support.annotation.NonNull android.graphics.Paint Paint)
使用指定的绘画绘制指定的圆形矩形。圆形矩形
将根据油漆中的样式填充或装框
参数:
1) rect-要绘制的roundRect的矩形边界
2) rx-用于圆角的椭圆形的x半径
3) ry-用于圆角的椭圆形的y半径
4) 绘制-用于绘制圆矩形的绘制
代码示例
设置为“活动”中的任何视图
RoundedRectangle roundedRectangle=new RoundedRectangle(ContextCompat.getColor(this,R.color.colorAccent));
textView.setBackground(roundedRectangle);
截图:
使用可从xml绘制的形状在您的情况下不起作用?也许我想,因为它们每次都是一样的。您能举个例子吗?我认为编程可能更好。我假设了更多的选项。为什么选择xml?您可以非常有效地自定义xml形状。是的,它也会让您的代码更干净。我已经尝试过了。是吗不适合我。因为我有很多对象。为了更好地编码,我应该将我的对象作为类。而不仅仅是canvas.drawSomething。无论如何,应用程序都会变得更大,这将使这种编码难以管理。我不明白你的意思……可以用它的参数为圆角矩形定义一个类(位置、大小…)当需要绘制时,请使用drawRoundRect
。您能提供一个java类示例吗?例如,通过canvas.drawRect
直接绘制到画布上是不好的。最好创建可以控制方法中所有参数的对象,每次只创建一个新对象。您可以尝试这样做:public class RoundR例如{int l,r,t,b,rx,ry;Paint Paint;public RoundRect(int l,int r,int t,int b,int rx,int ry,Paint Paint){this.l=l;this.r=r;this.t=t;this.b=b;this.Paint=Paint;}公共虚空绘制(画布c,Paint Paint Paint){c.drawRoundRect(l,t,r,b,rx,ry,Paint);}你能添加这个作为答案吗?我会尝试使用itExcellent答案。我将另一个标记为正确的,因为它对我的实现更有效,但是当我需要设置背景时,我肯定会使用你的答案。谢谢!希望你获得大量投票。很棒的信息,图片,非常有用。
myRects.add(new MyRect(touchx,touchy,100,100));
myRects.add(new MyRect(touchx,touchy,100,100,50));
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
/**
* Created by jinesh on 24/5/17.
*/
public class RoundedRectangle extends Drawable {
private Paint rectPaint;
private RectF drawableBounds;
public RoundedRectangle(int rectBackground) {
rectPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
rectPaint.setColor(rectBackground);
drawableBounds=new RectF();
}
@Override
public void draw(@NonNull Canvas canvas) {
Rect bounds=getBounds();
drawableBounds.set(bounds.left,bounds.top,bounds.right,bounds.bottom);
canvas.drawRoundRect(drawableBounds,10,10,rectPaint);
}
@Override
public void setAlpha(@IntRange(from = 0, to = 255) int alpha) {
rectPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
rectPaint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
RoundedRectangle roundedRectangle=new RoundedRectangle(ContextCompat.getColor(this,R.color.colorAccent));
textView.setBackground(roundedRectangle);