安卓:显示位图 [英] Android: Displaying Bitmap

查看:278
本文介绍了安卓:显示位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前那种问过这个项目,但我也碰到过,我想分享新的问题和新的想法。

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.

这是第一个活动code,它过渡到第二个活动code:

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

}

第二个活动code是:

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

}

被覆盖的onDraw:

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>

问题:点击从第一个活动的布局按钮后,转移到第二个屏幕是白屏。此外,在的logcat屏幕的底部,底部的M的波动从300M 80M至300M 110。此外,在上市的游戏布局应该怎样起草阶段我用这个值之间有什么文件夹名称或术语?在100,100和-30,-30图形值只是测试边界( - )。另外,Android的:在游戏中布局文本=@字符串/ ShowText不显示任何

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.

唯一的红色logcat的条目是间歇性的。

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,你应该申报的视图实例在GameActivity.java

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

第二删除的setContentView(R.layout.activity_game);

2nd remove setContentView(R.layout.activity_game);.

MainActivity.java

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

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

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

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

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>

这篇关于安卓:显示位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆