1

私はアンドロイドの初心者です:)

数値パラメーター (x) とボタンを使用して単純なスライド ショーを作成したいと考えています。 1 つの画像と次の画像は 1 秒待ちます。

最後に、操作中に経過した時間を知りたいです。(私の英語でごめんなさい)

アクティビティのコードとレイアウトの下に貼り付けます。

package com.superpibenchmarknative;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;



public class ImageActivity extends Activity implements OnClickListener {
private Button start;
private EditText iteractions;
private TextView time;
private ImageView display;
private long after,before;
private int images[] = {R.drawable.image1,R.drawable.image2,R.drawable.image3,
                        R.drawable.image4,R.drawable.image5,R.drawable.image6,R.drawable.image7,
                        R.drawable.image8,R.drawable.image9,R.drawable.image10,R.drawable.image11,
                        R.drawable.image12,R.drawable.image13,R.drawable.image14,R.drawable.image15,
                        R.drawable.image16,R.drawable.image17,R.drawable.image18,R.drawable.image19,
                        R.drawable.image20,R.drawable.image21,
        };
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image);
    setUpUI();
}

private void setUpUI() {
    // TODO Auto-generated method stub
    start = (Button) findViewById(R.id.bntStartCalcolation);
    iteractions = (EditText) findViewById(R.id.etIterations);
    time = (TextView) findViewById(R.id.tvTime);

    start.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
    //Creo le imageview in base al # di iteractions 
    before = System.currentTimeMillis();
    for (int j = 0; j < Integer.parseInt(iteractions.getText().toString());j++){
        display = (ImageView) findViewById(R.id.ivDisplay);
        display.setImageResource(images[j]);
        Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
             public void run() { 
                  display = null;
                  System.gc();
             } 
        }, 1000); 

    }
    display = null;
    System.gc();
    after = System.currentTimeMillis();
    time.setText(String.valueOf(after-before-1000* Integer.parseInt(iteractions.getText().toString())));
}

}

これはレイアウトです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".ImageActivity" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="10dp"
    android:padding="5dp"
    android:text="Benchmark del rendering di immagini" />

<EditText
    android:id="@+id/etIterations"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="5dp"
    android:hint="# di iterazioni"
    android:inputType="number"
    android:padding="5dp" />

<Button
    android:id="@+id/bntStartCalcolation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:text="Start Calcolation" />

<ImageView  android:id="@+id/ivDisplay"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:layout_weight="50"
        android:text="Risultato ottenuto in: " />

    <TextView
        android:id="@+id/tvTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:layout_weight="50" />
</LinearLayout>

このコードは機能しません。例外が発生します

OutOfMemoryError: ビットマップ サイズが VM の予算を超えています

誰でもこの問題を解決するのを手伝ってくれる?? :)どうもありがとう :D

素晴らしいすべての作品:Dしかし、もう少し質問があります

時間が常に負になる理由がわかりません!!! なぜだかわかる方いますか??【postDelayedが効かない?!? :( ]

で解決: android.os.SystemClock.sleep(1000);

別のエラー、画像の設定後に ImageView が変更されません。何か提案はありますか??

4

2 に答える 2

0

マニフェスト ファイルでアプリケーションにラージヒープを追加します。

<application ... android:largeHeap="true" >

または、写真を小さくします。使用している画面のサイズに応じてさまざまなサイズがある場合があります。これにより、すべてのメモリを占有する可能性が大幅に低くなります。Android の画面解像度については、このページを参照してください。

于 2012-12-04T17:56:30.470 に答える
0

開発者サイト内に、大きなビットマップ ファイルの読み込みに関する小さなガイドがあります。アイデアは、メモリに収まるように低解像度のファイルをロードすることです: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

于 2012-12-04T17:59:19.167 に答える