2014-03-05 43 views
-1

我之前曾問過這個項目,但我遇到了新的問題和想要分享的新想法。Android:顯示位圖

我想繪製大量的隨機間隔相同的圖形。

下面是過渡到第二活動代碼的第一個活動碼:

package com.category.tap; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.ImageButton; 

public class TitleActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_title); 

     ImageButton switchButton = (ImageButton) findViewById(R.id.play); 
     switchButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(TitleActivity.this, GameActivity.class); 
       startActivity(intent); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.title, menu); 
     return true; 
    } 

} 

第二個活動的代碼是:

package com.category.tap; 

import android.app.Activity;a 
import android.os.Bundle; 
import android.view.Menu; 

public class GameActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_game); 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.game, menu); 
     return true; 
    } 

} 

被覆蓋的onDraw:

package com.category.tap; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.view.View; 

public class DrawV extends View { 

    public DrawV(Context context){ 
     super(context); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Bitmap bit_dot = BitmapFactory.decodeResource(getResources(), R.drawable.dot_catch); 
     canvas.drawBitmap(bit_dot, 100, 100, null); 
     canvas.drawBitmap(bit_dot, -30, -30, null); 
    } 
} 

最後,遊戲活動的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".GameActivity" 
    android:text="@string/ShowText" > 

    <View 
     class="com.category.tap.DrawV" 
     android:layout_width="100dp" 
     android:layout_height="100dp" /> 

</RelativeLayout> 

問題:單擊第一個活動的佈局中的按鈕後,轉換到第二個屏幕是白色屏幕。此外,在logcat屏幕的底部,底部M的波動範圍從300M的80M到300M的110。此外,應列出所列出的遊戲佈局中的哪些文件夾名稱或期限之間的術語用於此值? 100,100和-30,-30圖形值只是測試邊界( - )。此外,Game Layout中的android:text =「@ string/ShowText」也不會顯示。

唯一的紅色logcat的條目是間歇性的

03-05 13:27:52.020: E/KINETO(243): KLOG082- 01 00 09 02 00 00 00 00 
03-05 13:27:55.020: E/KINETO(243): KLOG082- 01 00 09 02 00 00 00 00 
etc. 

感謝您的幫助。我很困難。

+0

@Melvelde嘿,說了自己的情況下,更好的...希望... – user963070

回答

1

的問題是:

首先你應該GameActivity.java

第二刪除的setContentView(R.layout.activity_game)申報的視圖實例;.

MainActivity.java

public class TitleActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_title); 

    Button switchButton = (Button) findViewById(R.id.button1); 
    switchButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(TitleActivity.this, 
        GameActivity.class); 
      startActivity(intent); 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.title, menu); 
    return true; 
}} 

GameActivity.java

public class GameActivity extends Activity { 

private DrawV drawView; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //Declare instance of DrawV ... 
    drawView = new DrawV(this); 
    // setContentView 
    setContentView(drawView); 
}} 

DrawV.java

public class DrawV extends View { 

private Bitmap bit_dot; 

public DrawV(Context context) { 
    super(context); 
    bit_dot = BitmapFactory.decodeResource(getResources(), 
      R.drawable.ic_launcher); 

} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    canvas.drawBitmap(bit_dot, 100, 100, null); 
    canvas.drawBitmap(bit_dot, 50, 50, null); 
}} 

activity_title.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".TitleActivity" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="41dp" 
    android:layout_marginTop="52dp" 
    android:text="Button" /> 

</RelativeLayout> 

acticity_game.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".GameActivity" 
> 

<View 
    class="com.category.tap.DrawV" 
    android:layout_width="100dp" 
    android:layout_height="100dp" /> 

</RelativeLayout> 
+0

噓嗨,這是行不通的,雖然我避免「第一,你應該申報的視圖實例在GameActivity。 Java的「。那是什麼意思? – user963070

+0

試圖聯繫你,@Eli Sh。不工作。不意味着討厭。 – user963070

+0

我更新了所有活動和xml文件的答案,請再次測試。 –