我试图在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