カスタムビューにカスタム図面を描画しています。描画機能の座標を変更せずに、このビューを画面の下半分に表示したいと思います。私はこれができることを知っていますが、私はそれをすることができません。助けてください。
これは私のXMLコードです:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1" >
<your.com.learn.game
android:id="@+id/game1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom" />
</ScrollView>
</RelativeLayout>
ゲームビューコード:
public class game extends View{
private Bitmap image;
private Paint paint;
private int x=0;private int baseY = 250;
public game(Context context,AttributeSet attr) {
super(context,attr);
//TODO Auto-generated constructor stub
//LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//layoutInflater.inflate(R.layout.example,this);
//this.inflate(context, R.layout.example, null);
image=Bitmap.createBitmap(BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher));
paint =new Paint();
paint.setColor(Color.BLUE);
}
public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawLine(90, baseY,200,baseY, paint);
canvas.drawLine(125,baseY,125,baseY-100,paint);
canvas.drawLine(110,baseY,125,baseY-15,paint);
canvas.drawLine(140,baseY,125,baseY-15,paint);
canvas.drawLine(125,baseY-100,175,baseY-100,paint);
canvas.drawLine(125,baseY-85,140,baseY-100,paint);
canvas.drawLine(175,baseY-100,175,baseY-75,paint);
canvas.drawOval(new RectF(170,(baseY-75),10,12),paint);
canvas.drawOval(new RectF(170,baseY-65,15,25), paint);
canvas.drawLine(160,baseY-65,170,baseY-60,paint);
canvas.drawLine(183,baseY-60,193,baseY-65,paint);
canvas.drawLine(165,baseY-30,170,baseY-45,paint);
canvas.drawLine(183,baseY-45,193,baseY-30,paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
boolean resizeWidth = widthSpecMode != MeasureSpec.UNSPECIFIED;
boolean resizeHeight = heightSpecMode != MeasureSpec.UNSPECIFIED;
}
public void update()
{
if(x < 200)
x++;
else
x=0;
}
}
ゲームビューがネストされたScrollviewに追加されたときのLogcat(以下のSamによって説明されています):
07-05 18:53:09.638: W/dalvikvm(858): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-05 18:53:09.649: E/AndroidRuntime(858): FATAL EXCEPTION: main
07-05 18:53:09.649: E/AndroidRuntime(858): java.lang.RuntimeException: Unable to start activity ComponentInfo{your.com.learn/your.com.learn.LearningActivity}: java.lang.NullPointerException
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-05 18:53:09.649: E/AndroidRuntime(858): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 18:53:09.649: E/AndroidRuntime(858): at android.os.Looper.loop(Looper.java:123)
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-05 18:53:09.649: E/AndroidRuntime(858): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 18:53:09.649: E/AndroidRuntime(858): at java.lang.reflect.Method.invoke(Method.java:507)
07-05 18:53:09.649: E/AndroidRuntime(858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-05 18:53:09.649: E/AndroidRuntime(858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-05 18:53:09.649: E/AndroidRuntime(858): at dalvik.system.NativeStart.main(Native Method)
07-05 18:53:09.649: E/AndroidRuntime(858): Caused by: java.lang.NullPointerException
07-05 18:53:09.649: E/AndroidRuntime(858): at your.com.learn.LearningActivity.onCreate(LearningActivity.java:42)
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-05 18:53:09.649: E/AndroidRuntime(858): ... 11 more
public class LearningActivity extends Activity {
/** Called when the activity is first created. */
private game g;
int i=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//For my button class
/*
MyButton myb = (MyButton)findViewById(R.id.mybutton1);
myb.setText("Hello Students");
myb.setTextSize(40);
**/
//For custom view
setContentView(R.layout.main);
//RelativeLayout r=(RelativeLayout) findViewById(R.id.relative1);
g=new game(this,null);
ViewGroup vgroup=(ViewGroup) findViewById(R.id.main);
vgroup.addView(g);
Thread mythread=new Thread(new UpdateThread());
mythread.start();
}
public Handler updatehandler=new Handler(){
/** Gets called on every message that is received */
// @Override
public void handleMessage(Message msg) {
// g.update();
g.invalidate();
super.handleMessage(msg);
}
};
public class UpdateThread implements Runnable
{
public void run() {
// TODO Auto-generated method stub
while(true){
//calling the handler
LearningActivity.this.updatehandler.sendEmptyMessage(0);
}
}
}
}