我試圖在Android中獲得2D圖形。 作爲一個例子,我想實現一個自定義繪製,並在我的活動表明它下面在Android中實現自定義繪圖
class myDrawable extends Drawable {
private static final String TAG = myDrawable.class.getSimpleName();
private ColorFilter cf;
@Override
public void draw(Canvas canvas) {
//First you define a colour for the outline of your rectangle
Paint rectanglePaint = new Paint();
rectanglePaint.setARGB(255, 255, 0, 0);
rectanglePaint.setStrokeWidth(2);
rectanglePaint.setStyle(Style.FILL);
//Then create yourself a Rectangle
RectF rectangle = new RectF(15.0f, 50.0f, 55.0f, 75.0f); //in pixels
Log.d(TAG,"On Draw method");
// TODO Auto-generated method stub
Paint paintHandl = new Paint();
// paintHandl.setColor(0xaabbcc);
paintHandl.setARGB(125, 234, 213, 34);
RectF rectObj = new RectF(5,5,25,25);
canvas.drawRoundRect(rectangle, 0.5f, 0.5f, rectanglePaint);
}
@Override
public int getOpacity() {
// TODO Auto-generated method stub
return 100;
}
@Override
public void setAlpha(int alpha) {
// TODO Auto-generated method stub
}
@Override
public void setColorFilter(ColorFilter cf) {
// TODO Auto-generated method stub
this.cf = cf;
}
}
我試圖得到儘可能提到
我已經從Android的繪製延長定義自定義繪製這顯示在我的活動,如下圖所示
public class custDrawable extends Activity {
/** Called when the activity is first created. */
LinearLayout layObj = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layObj = (LinearLayout) findViewById(R.id.parentLay);
ImageView imageView = (ImageView) findViewById(R.id.icon2);
myDrawable myDrawObj = new myDrawable();
imageView.setImageDrawable(myDrawObj);
imageView.invalidate();
// layObj.addView(myDrawObj, params);
}
}
但是當我運行的應用程序,我看到的活動沒有矩形,任何人都可以幫我嗎? 我哪裏錯了?
我做了修改提到casey,但我仍然無法看到任何有效的東西繪製在我的看法。 – Girish 2010-06-04 06:29:06
另一個問題是,它似乎永遠不會將'ImageView'添加到'LinearLayout'中。你需要添加'layObj.addView(imageView);'到'onCreate()'的末端' – CaseyB 2010-06-07 16:17:52
我已經添加了imageView到線性佈局,它基本上是一個設置界限的問題 在xml中,我硬編碼的寬度和高度現在它顯示出來了,所以我的setBounds有一些問題呢? – Girish 2010-06-10 16:38:53