Simple Drawable Problem and a few other questions

Put your problem here if it does not fit any of the other categories.

Simple Drawable Problem and a few other questions

Postby Poffy » Tue Mar 18, 2008 10:40 pm

I have some simple code that I'm trying to get working.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.poff.android.drawtest;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.content.Context;
  9.  
  10. import android.graphics.Bitmap;
  11.  
  12. import android.graphics.Canvas;
  13.  
  14. import android.graphics.Paint;
  15.  
  16. import android.graphics.drawable.Drawable;
  17.  
  18. import android.os.Bundle;
  19.  
  20. import android.view.View;
  21.  
  22. import android.view.Window;
  23.  
  24.  
  25.  
  26. public class DrawTest extends Activity {
  27.  
  28.  
  29.  
  30.     MyView mview;
  31.  
  32.  
  33.  
  34.    
  35.  
  36.     @Override
  37.  
  38.     public void onCreate(Bundle icicle) {
  39.  
  40.         super.onCreate(icicle);
  41.  
  42.        
  43.  
  44.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  45.  
  46.        
  47.  
  48.         mview = new MyView(this);
  49.  
  50.         setContentView(mview);
  51.  
  52.      
  53.  
  54.    
  55.  
  56.     }
  57.  
  58.     public class MyView extends View {
  59.  
  60.         private Drawable playerImage;
  61.  
  62.  
  63.  
  64.  
  65.  
  66.         private int playerWidth;
  67.  
  68.         private int playerHeight;
  69.  
  70.  
  71.  
  72.        
  73.  
  74.        
  75.  
  76.         public MyView(Context c) {
  77.  
  78.             super(c);
  79.  
  80.             playerImage = c.getResources().getDrawable(R.drawable.theplayer);
  81.  
  82.             playerWidth = playerImage.getIntrinsicWidth();
  83.  
  84.             playerHeight = playerImage.getIntrinsicHeight();            
  85.  
  86.  
  87.  
  88.         }
  89.  
  90.        
  91.  
  92.         @Override
  93.  
  94.         protected void onDraw(Canvas canvas) {
  95.  
  96.                 super.onDraw(canvas);
  97.  
  98.                 canvas.save();
  99.  
  100.                 playerImage.draw(canvas);
  101.  
  102.                 canvas.restore();
  103.  
  104.                
  105.  
  106.         }
  107.  
  108.        
  109.  
  110.        
  111.  
  112.     }
  113.  
  114.  
  115.  
  116. }
  117.  
  118.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


What am I doing wrong in this code? playerImage does not show up anywhere on the screen. Since I'm working on a game, is a Drawable the correct type to be used here? Or should I use ImageView? I plan on using animations, if that makes a difference.

Also, is there a link to a good tutorial that explains the XML resource files? Is there a tutorial on having multiple views at once (Tileview + other views)?
Poffy
Freshman
Freshman
 
Posts: 3
Joined: Tue Mar 18, 2008 10:29 pm

Top

Postby Poffy » Wed Mar 19, 2008 9:03 pm

I decided to start over a little bit, so sorry for the double post.

New code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.poff.android.stickman;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.widget.ImageView;
  11.  
  12.  
  13.  
  14. public class Stickman extends Activity {
  15.  
  16.         private ImageView stickImg;
  17.  
  18.     /** Called when the activity is first created. */
  19.  
  20.     @Override
  21.  
  22.     public void onCreate(Bundle icicle) {
  23.  
  24.         super.onCreate(icicle);
  25.  
  26.         stickImg = (ImageView)findViewById(R.id.stickman);
  27.  
  28.         DrawStickman();
  29.  
  30.         setContentView(R.layout.main);
  31.  
  32.     }
  33.  
  34.    
  35.  
  36.     public void DrawStickman() {
  37.  
  38.         ImageView image = new ImageView(this);
  39.  
  40.         image.setImageDrawable(getResources().getDrawable(R.drawable.stickman4));
  41.  
  42.         //image.setScaleType(ImageView.ScaleType.FIT_CENTER);
  43.  
  44.  
  45.  
  46.     }
  47.  
  48. }
  49.  
  50.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


And my simple 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:orientation="vertical"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent"
  11.  
  12.    >
  13.  
  14. <TextView  
  15.  
  16.    android:layout_width="fill_parent"
  17.  
  18.    android:layout_height="wrap_content"
  19.  
  20.    android:text=""
  21.  
  22.    />
  23.  
  24.    
  25.  
  26. <ImageView
  27.  
  28. android:id="@+id/stickman"
  29.  
  30.  
  31.  
  32. android:layout_centerVertical="true"
  33.  
  34.  
  35.  
  36. android:layout_centerHorizontal="true"
  37.  
  38.  
  39.  
  40. android:layout_width="80.0sp"
  41.  
  42.  
  43.  
  44. android:layout_height="160.0sp"
  45.  
  46.  
  47.  
  48. android:src="@drawable/stickman1"
  49.  
  50.  
  51.  
  52. />
  53.  
  54. </LinearLayout>
  55.  
  56.  
  57.  
  58.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


What happens, is when I start my app, I only one stickman image is drawn to the screen (the stickman image from the XML resource). How do I set up the DrawStickman() function to draw the extra stickman image, aswell?

Also, I'm still having some problems with XML resource loading. If I attempt to use stickImg or get any information from it, I get the NULL pointer error message.

I'm thinking this is a pretty simple mistake. Any ideas?
Poffy
Freshman
Freshman
 
Posts: 3
Joined: Tue Mar 18, 2008 10:29 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests