2015-09-17 67 views
1

嗨,我需要一個方向來踢開始。 我知道我可以創建一個矩形或圓形並在其中填充顏色。因此,我想要創建一個隨機形狀(如圖標圖像)並在其中填充顏色。什麼是正確的做法?我可以將圖像導入到畫布並在特定位置更改其顏色嗎?android:在Canvas中創建圖像並填充顏色

這裏是我迄今所做的: 所以我創建了一個帆布收到位圖 畫形狀(矩形和圓形),現在我想添加形狀像電話圖標並改變其顏色 我的代碼:

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



} 
+0

看看這個答案:http://stackoverflow.com/a/5208500/852049 – Karolis

+0

Karolis謝謝!你可以提出編輯問題嗎? –

回答

1

如果你正在繪製靜態形狀,我建議使用一個庫。

基於Font Awesome圖書館的網站,你可以嘗試this圖書館。這個想法是,你有靜態圖像作爲一種字體(矢量圖形很好地擴展),然後將其作爲文本對象在屏幕上繪製。它允許多種方便的定製

在你的佈局XML,這將是那麼容易,因爲:

<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..." 
    ... /> 

所以,你可以挑選從提供的庫隨機的形狀,並添加一個隨機的顏色。

+0

Karolis再次感謝!我可以製作一張我自己的矢量圖形,並在畫布內改變顏色 –

+0

您可以。 Google用於繪製填充的多邊形。另一個例子:http://stackoverflow.com/questions/2047573/how-to-draw-filled-polygon – Karolis