【问题标题】:Android Canvas: Create RoundRectShape ObjectAndroid Canvas:创建 RoundRectShape 对象
【发布时间】:2017-10-13 02:47:21
【问题描述】:

我为矩形创建了一个类,因为它是我的应用程序中的一个对象。但是现在我希望角落变圆。您可以在下面看到它是一个简单的类,可以创建具有相同属性的任意数量的矩形。

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

}

更新:没有这种方法,我什么都不会被绘制,因为没有大小。有了它,它只绘制通常的矩形。 (更改为不显示应用特定方法的整数值)

public void setDrawableAttributes(ShapeDrawable shapeDrawable){
   int x = 0;
   int y = 500
   int width = 200
   int height = 300
   shapeDrawable.setBounds(x, y-height, x + width,y+height );

}

根据我的研究,我发现我不能简单地添加圆角,而是必须创建一个RoundRectShape shapeDrawable。我使用这个RoundRectShape 创建一个圆角矩形的每一次尝试都失败了。不知何故,形状总是以没有圆角的规则矩形结束。

我正在寻找一个简单的骨架类(就像提供的那样),它可以创建一个 roundRectShape 可绘制对象。只要它有圆角,高度和宽度就无关紧要。必须使用 Java 而不是 XML。

我尝试创建圆角矩形的链接:

1.https://developer.android.com/reference/android/graphics/drawable/shapes/RoundRectShape.html

2.http://alvinalexander.com/java/jwarehouse/android/graphics/java/android/graphics/drawable/shapes/RoundRectShape.java.shtml

3.https://www.codota.com/android/scenarios/52c5d269da0a37e1836d6e75/android.graphics.drawable.shapes.RoundRectShape?tag=coyote

4.http://developer.oesf.biz/em/developer/reference/durian/android/graphics/drawable/shapes/RoundRectShape.html

5.https://android.googlesource.com/platform/frameworks/base/+/donut-release/graphics/java/android/graphics/drawable/shapes/RoundRectShape.java

6.Android: RoundRectShape: Modify corner radii

7.http://www.programcreek.com/java-api-examples/index.php?api=android.graphics.drawable.shapes.RoundRectShape

8.http://www.edumobile.org/android/shape-drawing-example-in-android/ 9.http://programtalk.com/java-api-usage-examples/android.graphics.drawable.shapes.RoundRectShape/

【问题讨论】:

  • 使用可从 xml 绘制的形状在您的情况下不起作用?
  • 也许我想因为它们每次都是一样的。能举个例子吗?
  • 我认为程序化可能仍然更好。我假设有更多选择。为什么要用 xml 代替?
  • 您可以非常有效地自定义 xml 形状。是的,它也会让你的代码更干净。
  • 我试过了。对我不起作用。

标签: java android android-canvas shapedrawable android-shapedrawable


【解决方案1】:

为什么不使用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);
    }
}`

【讨论】:

  • 因为我有很多对象。为了更好地编码,我应该将我的对象作为类。不仅仅是 canvas.drawSomething。无论如何,应用程序会变得更大,这将使这种编码难以管理。
  • 不明白你的意思...可以用它的参数(位置,大小...)为圆角矩形定义一个类,并在需要绘制时使用drawRoundRect它。
  • 你能提供一个java类的例子吗?例如,通过canvas.drawRect 直接在画布上绘制是不好的。最好创建可以控制方法中所有参数的对象,每次只创建一个新对象。
  • 你可以试试这个: public class RoundRect{ int l,r,t,b,rx,ry;油漆油漆;公共 RoundRect(int l,int r,int t,int b,int rx,int ry,油漆){ this.l=l;这个.r=r;这.t=t;这个.b=b; this.paint=油漆; } public void draw(Canvas c,Paint paint){ c.drawRoundRect(l,t,r,b,rx,ry,paint); } }
  • 你能添加这个作为答案吗?我会试试的
【解决方案2】:

我创建了一个类MyRect,用于为您绘制圆角矩形。

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

View/SurfaceViewonDraw(Canvas canvas)方法内部调用MyRect的draw()方法。

for(MyRect rect:myRects)
    rect.draw(canvas);

完成,创建对象并添加到容器中。

myRects.add(new MyRect(touchx,touchy,100,100)); 

myRects.add(new MyRect(touchx,touchy,100,100,50)); 

您还可以根据您的要求扩展MyRect,例如添加更多构造函数、方法和数据成员。

【讨论】:

    【解决方案3】:

    自定义可绘制对象

    你可以通过扩展drawable类来创建自定义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.TRANSLUCENT、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) 油漆 - 用于绘制圆形矩形的油漆

    代码示例

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

    截图:

    【讨论】:

    • 优秀的答案。我将另一个标记为正确,因为它更适合我的实现,但是当我需要设置背景时,我肯定会使用你的。谢谢!希望你能得到很多投票。很棒的信息,图片,非常有用。
    猜你喜欢
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多