Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

java - Android: Displaying Bitmap

I kind of asked this project before, but I have come across new problems and new ideas I want to share.

I am trying to draw a multitude of the same graphic randomly spaced.

Here is the first activity code that transitions to the second activity code:

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;
    }

}

The second activity code is:

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;
    }

}

The overwritten 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);
    }
}

Lastly, the layout for the game activity:

<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>

The problem: After clicking on the button from the first activity's layout, the transitioned to second screen is a white screen. Also, at the bottom of the logcat screen, the bottom M's fluctuate from 80M of 300M to 110 of 300M. Also, what in the listed game layout should be drafted for What folder names or terms between periods do I use for this value? The 100, 100 and -30, -30 graphic values are just testing boundaries (-). Also, android:text="@string/ShowText" in the Game Layout does not display either.

The only red logcat entries are intermittent

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.

Thanks for any help. I am really stuck.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is:

1st you should declare instance of view in GameActivity.java

2nd remove 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...