1

Hi I need a direction to Kick start. I know i can create a rectangle or circle and fill colors inside it.So, i want to create an Random shape (like an icon image) and fill Colors in it. What is the right approach to do?. Can I import an Image to my canvas and change its color in specific spots ?

Here Is what I have did so far: So I created a Canvas received bitmap drew shapes (rectangle and circle) now i want to add shape like telephone icon and change its color my code:

public class MainActivity extends Activity {
    Context ctx =this;
    Bitmap mcontactPhoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Bitmap bp =  drawTextToBitmap (this,R.drawable.bitmap_le_lo,"MMMM","blah",200);
        Drawable d = new BitmapDrawable(getResources(), bp);
        iv.setImageDrawable(d);

    }

    public Bitmap drawTextToBitmap(Context gContext,
                                   int gResId,
                                   String gText,String hText ,int TextSize ) {
        int m = TextSize;
        Resources resources = gContext.getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap bitmap =
                BitmapFactory.decodeResource(resources, gResId);

        android.graphics.Bitmap.Config bitmapConfig =
                bitmap.getConfig();
        // set default bitmap config if none
        if(bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        // resource bitmaps are imutable,
        // so we need to convert it to mutable one
        bitmap = bitmap.copy(bitmapConfig, true);

        Canvas canvas = new Canvas(bitmap);
        Paint paint =new Paint(Paint.ANTI_ALIAS_FLAG);
        Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(8);

        canvas.drawRect(71, 71, 320, 320, mPaint);
        canvas.drawRect(391, 71, 1189, 320, mPaint);
        canvas.drawRect(71, 390, 1189, 639, mPaint);

        canvas.drawCircle(71, 71, 40, paint);
        canvas.drawCircle(391, 71, 40, paint);
        canvas.drawCircle(71, 390, 40, paint);

        canvas.drawCircle(71, 71, 40, mPaint);
        canvas.drawCircle(391, 71, 40, mPaint);
        canvas.drawCircle(71, 390, 40, mPaint);


        int x = 420;
        int y = (249+m)/2+71/2;
        paint.setTextSize(m);
        paint.setColor(Color.BLUE);
        canvas.drawText(gText, x, y, paint);


        int x1 = 101;
        int y1 = (391-71/2)+(249+m)/2;
        paint.setTextSize(m);
        paint.setColor(Color.WHITE);
        canvas.drawText(hText, x1, y1, paint);



        paint.setTextSize(50);
        paint.setColor(Color.WHITE);
        canvas.drawText("1", 71-12.5f, 71+12.5f, paint);


        return bitmap;
    }



}
nikhil patil
  • 142
  • 1
  • 12

1 Answers1

1

If you are drawing static shapes, I suggest using a library.

Based on Font Awesome library for websites, you can try this library. The idea is that you have your static image as a font (vector graphics are nicely scalable) and then draw it on the screen as text object. It allows many easy customisations

In your layout XML this would be as easy as:

<com.joanzapata.iconify.widget.IconTextView
    android:text="I {fa-heart-o} to {fa-code} on {fa-android}"
    android:shadowColor="#22000000"
    android:shadowDx="3"
    android:shadowDy="3"
    android:shadowRadius="1"
    android:textSize="40sp"
    android:textColor="#FF..."
    ... />

So you could pick a random shape from the provided library and add a random colour.

Karolis
  • 1,558
  • 1
  • 14
  • 23
  • Karolis Thanks again! can i make a vector graphic of my own and change colors inside the canvas – nikhil patil Sep 17 '15 at 20:55
  • You can. Google for drawing filled polygons. Another example: http://stackoverflow.com/questions/2047573/how-to-draw-filled-polygon – Karolis Sep 17 '15 at 20:57