I have a LinearLayout with two views inside, the inside view is a custom view.
Drawing problem I am facing, my custom view just draws six rects on itself, please see RecView source code.
It is only one view can be drawn on screen, but I aslo used hierarchy viewer to check layout correctly,
two custom views are also shown on viewer ().
emulator and hierarchy viewer screen capture are shown as bellow link.
http://picasaweb.google.com/tw.ahhsin/A ... directlink
Anyone know what problem is my program? thanks for any responses.
RecView.java
Using java Syntax Highlighting
- public class RecView extends View {
- final private static String TAG = "RecView";
- private Paint mPaint;
- private int mCount;
- public RecView(Context context, int count) {
- super(context);
- mPaint = new Paint();
- mPaint.setColor(getResources().getColor(R.color.background));
- mCount = count;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- int left = getLeft();
- int top = getTop();
- int right = getRight();
- int bottom = getBottom();
- Rect rec1 = new Rect(0, top, 50, bottom);
- Rect rec2 = new Rect(50, top, 100, bottom);
- Rect rec3 = new Rect(100, top, 150, bottom);
- Rect rec4 = new Rect(150, top, 200, bottom);
- Rect rec5 = new Rect(200, top, 250, bottom);
- Rect rec6 = new Rect(250, top, 300, bottom);
- //Drawing six rects
- mPaint.setColor(getResources().getColor(R.color.solid_blue));
- canvas.drawRect(rec1, mPaint);
- canvas.drawRect(rec3, mPaint);
- canvas.drawRect(rec5, mPaint);
- mPaint.setColor(getResources().getColor(R.color.solid_red));
- canvas.drawRect(rec2, mPaint);
- canvas.drawRect(rec4, mPaint);
- canvas.drawRect(rec6, mPaint);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- setMeasuredDimension(300, 50);
- }
- @Override
- protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
- if(changed && left >= (320 - left)) {
- int newleft = 0;
- int newTop = (mCount-1)*50;
- int newBottom = mCount*50;
- layout(newleft, newTop, 300, newBottom);
- super.onLayout(changed, newleft, newTop, 300, newBottom);
- } else {
- super.onLayout(changed, left, top, right, bottom);
- }
- requestLayout();
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
RecViewActivity.java
Using java Syntax Highlighting
- public class RecViewActivity extends Activity {
- public RecViewActivity() {}
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- @Override
- protected void onResume() {
- super.onResume();
- LinearLayout layout = (LinearLayout) findViewById(R.id.main_view);
- layout.setWillNotDraw(false);
- RecView view = null;
- for(int i = 1; i < 3; i++) {
- view = new RecView(this, i);
- layout.addView(view);
- }
- }
- }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


