Android: Displaying Bitmap

笑着哭i 提交于 2019-12-13 08:15:21

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/22207722/android-displaying-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!