最新消息:欢迎访问Android开发中文站!商务联系QQ:1304524325

AssetManager读取assets下多张图片资源输出到ImageView动画

热点资讯 loading 436浏览 0评论

这里面有几个要点和环节需要注意:

1,AssetManager读取事先放置到assets目录下的原始图片资源文件,组装成Android的Bitmap数组。
文件结构如图:

2,把1中读取到的Bitmap数组每隔一个较小时间内(如25ms)不间断循环设置到ImageView里面,从而在视觉上形成一种动画效果。

代码:

<br /><br />package zhangphil.test;<br /><br />import android.content.res.AssetManager;<br />import android.graphics.Bitmap;<br />import android.graphics.BitmapFactory;<br />import android.os.Bundle;<br />import android.support.annotation.Nullable;<br />import android.support.v7.app.AppCompatActivity;<br />import android.widget.ImageView;<br /><br />import java.io.InputStream;<br />import java.util.concurrent.TimeUnit;<br /><br />public class AnimationActivity extends AppCompatActivity {<br /><%%KEEPWHITESPACE%%> private boolean mStartLoadingAnimation = false;<br /><%%KEEPWHITESPACE%%> private ImageView mImageView;<br /><br /><%%KEEPWHITESPACE%%> @Override<br /><%%KEEPWHITESPACE%%> protected void onCreate(@Nullable Bundle savedInstanceState) {<br /><%%KEEPWHITESPACE%%> super.onCreate(savedInstanceState);<br /><%%KEEPWHITESPACE%%> setContentView(R.layout.animation_activity);<br /><%%KEEPWHITESPACE%%> mImageView = findViewById(R.id.image);<br /><br /><%%KEEPWHITESPACE%%> mStartLoadingAnimation = true;<br /><%%KEEPWHITESPACE%%> loadingAnimation();<br /><%%KEEPWHITESPACE%%> }<br /><br /><%%KEEPWHITESPACE%%> private void loadingAnimation() {<br /><%%KEEPWHITESPACE%%> new Thread(new Runnable() {<br /><%%KEEPWHITESPACE%%> @Override<br /><%%KEEPWHITESPACE%%> public void run() {<br /><%%KEEPWHITESPACE%%> Bitmap[] bitmaps = getBimaps();<br /><%%KEEPWHITESPACE%%> if (bitmaps == null || bitmaps.length == 0) {<br /><%%KEEPWHITESPACE%%> return;<br /><%%KEEPWHITESPACE%%> }<br /><br /><%%KEEPWHITESPACE%%> int i = 0;<br /><%%KEEPWHITESPACE%%> while (mStartLoadingAnimation) {<br /><%%KEEPWHITESPACE%%> mImageView.setImageBitmap(bitmaps[i++ % bitmaps.length]);<br /><br /><%%KEEPWHITESPACE%%> try {<br /><%%KEEPWHITESPACE%%> TimeUnit.MILLISECONDS.sleep(25);<br /><%%KEEPWHITESPACE%%> } catch (InterruptedException e) {<br /><%%KEEPWHITESPACE%%> e.printStackTrace();<br /><%%KEEPWHITESPACE%%> }<br /><%%KEEPWHITESPACE%%> }<br /><%%KEEPWHITESPACE%%> }<br /><%%KEEPWHITESPACE%%> }).start();<br /><%%KEEPWHITESPACE%%> }<br /><br /><%%KEEPWHITESPACE%%> private Bitmap[] getBimaps() {<br /><%%KEEPWHITESPACE%%> final String parentPath = "loading";<br /><br /><%%KEEPWHITESPACE%%> Bitmap[] bitmaps = null;<br /><%%KEEPWHITESPACE%%> AssetManager am = getAssets();<br /><%%KEEPWHITESPACE%%> try {<br /><%%KEEPWHITESPACE%%> String[] files = am.list(parentPath);<br /><%%KEEPWHITESPACE%%> bitmaps = new Bitmap[files.length];<br /><%%KEEPWHITESPACE%%> for (int i = 0; i &lt; files.length; i++) {<br /><%%KEEPWHITESPACE%%> InputStream is = am.open(parentPath + "/" + files[i]);<br /><%%KEEPWHITESPACE%%> bitmaps[i] = BitmapFactory.decodeStream(is);<br /><%%KEEPWHITESPACE%%> is.close();<br /><%%KEEPWHITESPACE%%> }<br /><%%KEEPWHITESPACE%%> } catch (Exception e) {<br /><%%KEEPWHITESPACE%%> e.printStackTrace();<br /><%%KEEPWHITESPACE%%> }<br /><br /><%%KEEPWHITESPACE%%> return bitmaps;<br /><%%KEEPWHITESPACE%%> }<br />}<br /><br />

转载请注明:Android开发中文站 » AssetManager读取assets下多张图片资源输出到ImageView动画

您必须 登录 才能发表评论!