我希望能够GIF动画加载到文本之间的TextView的帮助,这是我想出来的:在试图GIF动画里面的TextView
我用Html.fromHtml
由于一个GIF可能会出现多次,因此我会加载所有不同的GIF数组,以便我可以通过参考回(尽管在此测试程序中未使用)
这就是我所得到的到目前为止:
public class MainActivity extends Activity implements ImageGetter {
private TextView mTv;
private Map<String, LevelListDrawable> gifs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gifs = new HashMap<>();
String imgs="<p><img alt=\"\" src=\"http://lonelytraveller.comoj.com/qff/tjatja.gif\" style=\"height:50px; width:100px\" />Test Article, Test Article, Test Article, Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,Test Article,v</p>";
Spanned spanned = Html.fromHtml(imgs, this, null);
mTv = (TextView) findViewById(R.id.tv);
mTv.setText(spanned);
}
@Override
public Drawable getDrawable(String source) {
if(gifs.get(source.split("\\Q/\\E")[source.split("\\Q/\\E").length-1]) != null){
return gifs.get(source.split("\\Q/\\E")[source.split("\\Q/\\E").length-1]);
}else {
LevelListDrawable d = new LevelListDrawable();
Drawable empty = ContextCompat.getDrawable(this, R.drawable.save);
d.addLevel(0, 0, empty);
d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
new LoadImage().execute(source, d);
return d;
}
}
class LoadImage extends AsyncTask<Object, Void, byte[]> {
private LevelListDrawable mDrawable;
private String key;
@Override
protected byte[] doInBackground(Object... params) {
String source = (String) params[0];
key = source.split("\\Q/\\E")[source.split("\\Q/\\E").length-1];
mDrawable = (LevelListDrawable) params[1];
try {
InputStream is = new URL(source).openStream();
return IOUtils.toByteArray(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(byte[] bytes) {
if(bytes != null) {
GifDecoder decoder = new GifDecoder();
decoder.read(bytes);
ArrayList<Long> alist = new ArrayList<>();
for (int i = 0; i < decoder.getFrameCount(); i++) {
decoder.advance();
Bitmap bitmap = decoder.getNextFrame();
BitmapDrawable d = new BitmapDrawable(getResources(), bitmap);
int i2 = i+1;
mDrawable.addLevel(i2, i2, d);
mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
alist.add((long) decoder.getNextDelay());
}
gifs.put(key, mDrawable);
animateGif(decoder.getFrameCount(), mDrawable, alist);
}
}
}
public void animateGif(int length, LevelListDrawable d, ArrayList<Long> delays){
for(int i = 1; i<(length+1); i++){
d.setLevel(i);
CharSequence t = mTv.getText();
mTv.setText(t);
long sleep = delays.get(i-1);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(i == length) i=1;
}
}
}
我用这个GifDecoder:https://gist.github.com/devunwired/4479231
随着我上面贴的代码,我只得到空的绘制(0级),如果我在animateGif功能,我可以看到放一个记录,他确实做到了无限循环,但它只是不起作用,它停留在空的可绘制图像;
如果我改变onPostExecute功能的含量:
if(bytes != null) {
GifDecoder decoder = new GifDecoder();
decoder.read(bytes);
decoder.advance();
Bitmap bitmap = decoder.getNextFrame();
BitmapDrawable d = new BitmapDrawable(getResources(), bitmap);
mDrawable.addLevel(1, 1, d);
mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
mDrawable.setLevel(1);
CharSequence t = mTv.getText();
mTv.setText(t);
}
它确实改变了空图像转换成GIF文件的第一帧。 我只是不明白为什么这不起作用,任何人都可以帮助我吗?
调试器可以帮助您。 lib作者也可以帮助你。 – Shark