Problem with custom view

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem with custom view

Postby marsdroid » Wed Apr 22, 2009 9:48 am

Hi,
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
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class RecView extends View {
  3.  
  4.         final private static String TAG = "RecView";
  5.  
  6.         private Paint mPaint;
  7.  
  8.        
  9.  
  10.         private int mCount;
  11.  
  12.        
  13.  
  14.         public RecView(Context context, int count) {
  15.  
  16.                 super(context);
  17.  
  18.                 mPaint = new Paint();
  19.  
  20.                 mPaint.setColor(getResources().getColor(R.color.background));
  21.  
  22.                 mCount = count;
  23.  
  24.         }
  25.  
  26.  
  27.  
  28.         @Override
  29.  
  30.         protected void onDraw(Canvas canvas) {
  31.  
  32.                 super.onDraw(canvas);
  33.  
  34.                
  35.  
  36.                 int left = getLeft();
  37.  
  38.                 int top = getTop();
  39.  
  40.                 int right = getRight();
  41.  
  42.                 int bottom = getBottom();
  43.  
  44.                 Rect rec1 = new Rect(0, top, 50, bottom);
  45.  
  46.                 Rect rec2 = new Rect(50, top, 100, bottom);
  47.  
  48.                 Rect rec3 = new Rect(100, top, 150, bottom);
  49.  
  50.                 Rect rec4 = new Rect(150, top, 200, bottom);
  51.  
  52.                 Rect rec5 = new Rect(200, top, 250, bottom);
  53.  
  54.                 Rect rec6 = new Rect(250, top, 300, bottom);
  55.  
  56.                
  57.  
  58.                 //Drawing six rects
  59.  
  60.                 mPaint.setColor(getResources().getColor(R.color.solid_blue));
  61.  
  62.                 canvas.drawRect(rec1, mPaint);
  63.  
  64.                 canvas.drawRect(rec3, mPaint);
  65.  
  66.                 canvas.drawRect(rec5, mPaint);
  67.  
  68.                 mPaint.setColor(getResources().getColor(R.color.solid_red));
  69.  
  70.                 canvas.drawRect(rec2, mPaint);
  71.  
  72.                 canvas.drawRect(rec4, mPaint);
  73.  
  74.                 canvas.drawRect(rec6, mPaint);
  75.  
  76.                
  77.  
  78.         }
  79.  
  80.  
  81.  
  82.         @Override
  83.  
  84.         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  85.  
  86.                 setMeasuredDimension(300, 50);
  87.  
  88.         }
  89.  
  90.  
  91.  
  92.         @Override
  93.  
  94.         protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  95.  
  96.                
  97.  
  98.                 if(changed && left >= (320 - left)) {
  99.  
  100.                         int newleft = 0;
  101.  
  102.                         int newTop = (mCount-1)*50;
  103.  
  104.                         int newBottom = mCount*50;
  105.  
  106.                         layout(newleft, newTop, 300, newBottom);
  107.  
  108.                         super.onLayout(changed, newleft, newTop, 300, newBottom);      
  109.  
  110.                 } else {
  111.  
  112.                         super.onLayout(changed, left, top, right, bottom);     
  113.  
  114.                 }
  115.  
  116.                
  117.  
  118.                 requestLayout();
  119.  
  120.         }
  121.  
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


RecViewActivity.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class RecViewActivity extends Activity {
  3.  
  4.  
  5.  
  6.         public RecViewActivity() {}
  7.  
  8.  
  9.  
  10.         @Override
  11.  
  12.         protected void onCreate(Bundle savedInstanceState) {
  13.  
  14.                 super.onCreate(savedInstanceState);
  15.  
  16.                
  17.  
  18.                 setContentView(R.layout.main);
  19.  
  20.         }
  21.  
  22.  
  23.  
  24.         @Override
  25.  
  26.         protected void onResume() {
  27.  
  28.                 super.onResume();
  29.  
  30.                
  31.  
  32.                 LinearLayout layout = (LinearLayout) findViewById(R.id.main_view);
  33.  
  34.                 layout.setWillNotDraw(false);
  35.  
  36.                 RecView view = null;
  37.  
  38.                 for(int i = 1; i < 3; i++) {
  39.  
  40.                         view = new RecView(this, i);
  41.  
  42.                         layout.addView(view);
  43.  
  44.                 }      
  45.  
  46.         }
  47.  
  48. }
  49.  
  50.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
marsdroid
Freshman
Freshman
 
Posts: 2
Joined: Wed Apr 22, 2009 9:40 am

Top

Postby marsdroid » Wed Apr 22, 2009 10:33 am

Here is my layout xml

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.         android:id="@+id/main_view" android:clipChildren="true"
  7.  
  8.         android:layout_height="wrap_content" android:layout_width="wrap_content"
  9.  
  10.         android:orientation="vertical">
  11.  
  12.  
  13.  
  14.  
  15.  
  16. </LinearLayout>
  17.  
  18.  
  19.  
  20.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
marsdroid
Freshman
Freshman
 
Posts: 2
Joined: Wed Apr 22, 2009 9:40 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests