Problem in Canvas

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

Problem in Canvas

Postby Niketa » Mon Nov 24, 2008 9:07 am

Hi,
i want to use basic views like text view, button view, List view to draw in canvas.

I dont know its possible or not. if its possible then how?

I also saw link in this forum to add view in a view but i cant understand more. also saw the forum of "Stateful

CustomImageButton with programatic Draw" but cant succeed.

Please if any one know then tell me.

Thanks in advance.
Niketa
Developer
Developer
 
Posts: 36
Joined: Wed Sep 17, 2008 12:37 pm

Top

Postby Jbeer » Wed Nov 26, 2008 11:46 am

You can use a ViewGroup and add imageViews and TextViews to this ViewGroup, then, once you have the ViewGroup constructed you can call

dispatchDraw(canvas);

to draw the Viewgroup in the canvas.

Hint. You should do .layout() on every children you insert in the ViewGroup.

Hope this helps
Jbeer
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Jul 22, 2008 8:07 am
Location: España

Postby Niketa » Wed Nov 26, 2008 1:20 pm

Thanks for reply,
I have tried but not succeed. its not display any thing. here my code is,

Code: Select all

public class start0 extends Activity {
   
   String msg="Hi, I am Merlin.Your New Game Teacher";

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      View panel = RnD1();
      setTitle("Wel come");
      
      setTitleColor(Color.BLUE);
      
      Window w  = getWindow();                     
      w.requestFeature(Window.FEATURE_LEFT_ICON);   
      setContentView(panel);
      w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.favorites);
      
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      MenuItem item1 = menu.add(0, 1, 0, "Start");
      item1.setIcon(R.drawable.alert);
      return super.onCreateOptionsMenu(menu);
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      if (item.getTitle().equals("Start")) {

         Intent start = new Intent(this, start.class);
         startActivity(start);
         this.finish();
      
      }
      return super.onOptionsItemSelected(item);
   }
   
   
   public ViewGroup RnD1() {
      
      LinearLayout panel = new LinearLayout(this);
      panel.setOrientation(LinearLayout.VERTICAL);
      
      ImView v = new ImView(this);
      panel.addView(v);
      return panel;
   
   }
      
   
   public class ImView extends ViewGroup {
      Movie movie;
      InputStream is = null;
      long moviestart;
      Shader shader;
      
      public ImView(Context context) {
         super(context);
         is = context.getResources().openRawResource(R.drawable.cartoon);
         movie = Movie.decodeStream(is);
         
         LinearLayout panel1 = new LinearLayout(start0.this);
         panel1.setOrientation(LinearLayout.VERTICAL);
         
         TextView t = new TextView(start0.this);
         t.setText("Wel come in view group");
         t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
               LayoutParams.WRAP_CONTENT));
         
         panel1.addView(t);
         
      }

      @Override
      protected void onDraw(Canvas canvas) {
            dispatchDraw(canvas);
         
      /*   long now = android.os.SystemClock.uptimeMillis();
         Bitmap bm = null;
         setBackgroundColor(Color.WHITE);
         if (moviestart == 0) { // first time
            moviestart = now;
         }
         Paint p = new Paint();
         p.setTextSize(18);
         p.setColor(Color.BLACK);
         p.setAntiAlias(true);
         p.setAlpha(250);
         Typeface face = Typeface.createFromAsset(getAssets(),
               "BRADHITC.TTF");
         p.setTypeface(face);
         shader = new LinearGradient(0, 0, 200, 0, new int[] {
               Color.argb(255, 120, 120, 120),
               Color.argb(255, 120, 120, 120),
               Color.argb(255, 255, 255, 255) },
               new float[] { 0, 0.7f, 1 }, TileMode.MIRROR);
         p.setShader(shader);
         
         int relTime = (int) ((now - moviestart) % movie.duration());
         movie.setTime(relTime);
         movie.draw(canvas, 150, 120);
         Log.i("tag","Movie duration"+movie.duration());
         bm = BitmapFactory.decodeResource(this.getResources(),
               R.drawable.mes);

         canvas.drawBitmap(bm, 0, 0, new Paint());

         long lastTime = 0;
         float elapsed = (now - lastTime) / 7;
         float dx = elapsed;
         Matrix matrix = new Matrix();
         matrix.setTranslate(dx, 0);
         shader.setLocalMatrix(matrix);
         canvas.drawText(msg,0,16,40,50, p);
         canvas.drawText(msg,16,37,05,70, p);
         lastTime = now;
*/
         //this.invalidate();
      }

      @Override
      protected void onLayout(boolean changed, int l, int t, int r, int b) {
          int count = getChildCount();
             for (int i = 0; i < count; i++) {
                  View child = getChildAt(i);
                  if (child.getVisibility() != GONE) {
                       LayoutParams lp = (LayoutParams) child.getLayoutParams();
                       int cl = 100;
                       int ct = 100;
                       int cr = 100;
                       int cb = 100;
                       child.layout(cl, ct, cr, cb);
                  }
             }
         
      }

   }
}




please tell me solution if possible.
Niketa
Developer
Developer
 
Posts: 36
Joined: Wed Sep 17, 2008 12:37 pm

Postby Jbeer » Wed Nov 26, 2008 1:32 pm

Im working in something similar...

Have you tried to do the "layout" on every child but outside the onLayout?

Anyway I am not sure if that is valid.
Jbeer
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Jul 22, 2008 8:07 am
Location: España

Postby Niketa » Wed Nov 26, 2008 1:36 pm

hi,
I think you are telling to set layout parameters of component. so i have set it in the view group only at the time of defining it. but if you telling to call layout() and from that onLayout to each component then i hv tried on that but not succeed.

if you are succeed then tell me please.
anyways thanks for help.
Niketa
Developer
Developer
 
Posts: 36
Joined: Wed Sep 17, 2008 12:37 pm

Postby Jbeer » Wed Nov 26, 2008 1:57 pm

I was looking your code more carefully.

Tell me if Im wrong, but, do you add the LinearLayout to the GroupView in the ImView constructor?

Because you create a LinearLayout, and you add to it a TextView...but nothing happens with the LinerLayout.
Jbeer
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Jul 22, 2008 8:07 am
Location: España

Top

Postby Niketa » Wed Nov 26, 2008 2:12 pm

hi,
Actually we have to add view in view group not layout.
and i think i am right with that.
i have declared panel1 as linear layout and then also i have declared text view in its own different layout and then
add it to my main layout.

Thanks but i think its right but the problem is some where else.
Niketa
Developer
Developer
 
Posts: 36
Joined: Wed Sep 17, 2008 12:37 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 12 guests