【问题标题】:Android: Displaying BitmapAndroid:显示位图
【发布时间】:2014-03-05 19:48:08
【问题描述】:

我以前问过这个项目,但我遇到了新问题和新想法,我想分享一下。

我正在尝试以随机间隔绘制大量相同的图形。

这是转换到第二个活动代码的第一个活动代码:

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 图形值只是测试边界 (-)。此外,游戏布局中的 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.

感谢您的帮助。我真的被困住了。

【问题讨论】:

  • @Melvelde 嘿,更好地说明我的情况......希望......

标签: java android layout view ondraw


【解决方案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>

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

【讨论】:

  • 嘘 嘿,它不起作用,虽然我避免了“第一个你应该在 GameActivity.Java 中声明视图实例”。这是什么意思?
  • 试图联系你,@Eli Sh。不工作。不要讨厌。
  • 我用所有活动和xml文件更新了答案,请再次测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 2011-06-23
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 1970-01-01
相关资源
最近更新 更多