Próbowałem uzyskać grafikę 2D w systemie Android. Jako przykład chcę zaimplementować niestandardową rozciągliwej i pokazać go w mojej działalnościImplementowanie niestandardowego losowania w systemie Android
I zdefiniowano dostosowane rozciągliwej rozszerzając z Android rozciągliwej jak wspomniano poniżej
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;
}
}
Próbuję uzyskać ten wyświetlany w moja aktywność, jak pokazano poniżej
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);
}
}
Ale gdy uruchamiam aplikację nie widzę prostokąt o działalności, może ktoś mi pomóc? Gdzie się mylę?
Zrobiłem zmiany wspomniane przez Casey, ale nadal nie widzę żadnych ważnych rzeczy narysowanych na moim widoku. – Girish
Kolejnym problemem jest to, że nigdy nie dodajesz 'ImageView' do twojego' LinearLayout'. Musisz dodać 'layObj.addView (imageView);' na końcu 'onCreate()' – CaseyB
Dodałem imageView do układu liniowego, jest to w zasadzie problem z ustawionymi granicami W xml, ustawiłem na sztywno szerokość i wysokość widoku obrazu, teraz jest wyświetlany, więc jakiś problem z moimi setBounds to jest? – Girish