プロが教えるわが家の防犯対策術!

多数の画像を、パラパラ漫画のように切り替えていく方法について相談があり、投稿させていただきました。

以前、xmlファイルで画像を読み込む方法が簡単だということで、hirotn様にご回答いただき、
動作させることができました。
ありがとうございました。

携帯の実機にインストールし、エラーも出ず動作することも確認できたのですが、
画像を切り替えるタイミングがあまりにも遅くなっており困っています。

画像が多い場合は、メモリ不足等起こっているのでしょうか。
プログラムで解決できる方法があれば教えていただきたく、よろしくお願いします。

(ちなみに、xmlファイルで読み出すファイルは100枚程度(80MB程度)です。
もっと数を増やしたり、画質をよくしたいとも思っております。)

※サンプルは下記で見つけました。
http://monoist.atmarkit.co.jp/mn/articles/1205/2 …



(以下、プログラム)

package jp.test.animation.frame;

import android.app.Activity;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;



public class FrameAnimationTestActivity extends Activity {

// ボタン
Button mBtnAnimation;
Button m5BtnAnimFromXML;

// ビュー
ImageView mImageAnimation;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mBtnAnimation = (Button) findViewById( R.id.button_animation );
m5BtnAnimFromXML = (Button) findViewById( R.id.button_anim_from_xml );
mImageAnimation = (ImageView) findViewById( R.id.image_animation );

mBtnAnimation.setOnClickListener(mClickListener);
m5BtnAnimFromXML.setOnClickListener(mClickListener);
}


View.OnClickListener mClickListener = new View.OnClickListener() {
public void onClick(View v) {
// アニメーション中なら、停止
Drawable d = mImageAnimation.getBackground();
if( d != null ){
try{
if( ((AnimationDrawable) d).isRunning() ){
((AnimationDrawable) d).stop();
return;
}
}
catch( RuntimeException e ){
e.printStackTrace();
}
}



//if( v == mBtnAnimation ){
//frameAnimationTest(
//FrameAnimationTestActivity.this, mImageAnimation );
//}
if( v == m5BtnAnimFromXML ){
frameAnimationFromXMLTest( mImageAnimation );
}
}
};



// フレームアニメーションを XML から読み込む
void frameAnimationFromXMLTest( View v ){

// リソースからアニメーションを読み込み、ビューに設定
v.setBackgroundResource( R.drawable.droid_jump );

// ビューからアニメーションを取り出し
AnimationDrawable anim = (AnimationDrawable)v.getBackground();



// アニメーション開始
anim.start();
}
}

A 回答 (1件)

http://developer.android.com/reference/android/g …

<animation-list android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/wheel0" android:duration="50" />

AnimationDrawableのXMLを見ると、duration要素の値(ミリ秒で指定します)を短くすればよさそうです。
XMLが示されていないので一番思い当たるところとして回答します。
    • good
    • 0
この回答へのお礼

回答いただきありがとうございます。

XMLを記載しておりませんでしたが、DURATION=200msで設定しております。
画像50枚程度まではうまく動作したのですが、100にすると極端に遅くなってしまいました。

OUT OF MEMORY のエラーが表示するわけではないので、メモリ不足が原因かはわかりませんが。

下記のサイトでは、メモリを解放するプログラムが乗っておりました。
しかし上記プログラムにどう組み込めば良いかわからず、困っております。

もしわかるようであれば、教えていただけないでしょうか。


http://seesaakyoto.seesaa.net/article/365557192. …



private AnimationDrawable animation;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView image = (ImageView) this.findViewById(R.id.image);

animation = new AnimationDrawable();
// Bitmap生成時のオプション。
BitmapFactory.Options options = new Options();
// inPurgeableでBitmapを再利用するかどうかを明示的に決定
options.inPurgeable = true;
TypedArray images = getResources().obtainTypedArray(
R.array.animation_drawable);
for (int i = 0; i < images.length(); i++) {
animation.addFrame(
new BitmapDrawable(getResources(), BitmapFactory
.decodeResource(getResources(),
images.getResourceId(i, R.drawable.anim_1),
options)), 50);

}
// setBackgroundDrawableは非推奨
image.setImageDrawable(animation);
// ImageView全体に画像を拡大して表示する
image.setScaleType(ImageView.ScaleType.FIT_XY);
images.recycle();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
//onCreate上ではアニメーションがスタートしない
animation.start();
}




?

arrays.xml

<resources>

<array name="animation_drawable">
<item>@drawable/anim_1</item>
<item>@drawable/anim_2</item>
<!-- ~省略~ -->
<item>@drawable/anim_20</item>
</array>

</resources>

お礼日時:2013/10/01 22:46

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!