Java Android-动画gif作为电影冻结,只显示图像的一小部分
我在扩展View类以创建同时播放两个GIF的视图时遇到了困难。到目前为止,我已经成功地从资产加载了两个GIF,但我需要应用程序从几个URL加载它们。这就是应用程序现在显示的,两个GIF没有移动,实际上只是显示了它们的一小部分 这是视图的代码:Java Android-动画gif作为电影冻结,只显示图像的一小部分,java,android,view,gif,Java,Android,View,Gif,我在扩展View类以创建同时播放两个GIF的视图时遇到了困难。到目前为止,我已经成功地从资产加载了两个GIF,但我需要应用程序从几个URL加载它们。这就是应用程序现在显示的,两个GIF没有移动,实际上只是显示了它们的一小部分 这是视图的代码: import android.content.Context; import android.graphics.Canvas; import android.graphics.Movie; import android.graphics.Paint; i
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.View;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class GifView extends View {
long movieStart = 0;
boolean processFinish = false;
Movie [] bothMovies;
boolean staticMode = false;
private boolean done = false;
Context ctx;
public GifView(Context context) {
super(context);
new GetGifFromNetwork().execute();
bothMovies = new Movie[2];
ctx = context;
}
public GifView(Context context, AttributeSet attrs){
super(context, attrs);
new GetGifFromNetwork().execute();
bothMovies = new Movie[2];
ctx = context;
}
public GifView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
new GetGifFromNetwork().execute();
bothMovies = new Movie[2];
ctx = context;
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
System.out.println("Drawing view");
if(staticMode){
System.out.println("Starting stuff, very quickly");
getTheStuffInTheCanvas(canvas);
}
else{
while(!done){
if(processFinish){
System.out.println("Starting stuff");
getTheStuffInTheCanvas(canvas);
done = true;
staticMode = true;
}
}
}
}
class GetGifFromNetwork extends AsyncTask<Void, Integer, Movie[]> {
protected Movie[] doInBackground(Void... objects) {
InputStream in,in2;
System.out.println("Getting movie");
try {
System.out.println("Decoding movie 1");
Movie movie1 = urlToMovie(new URL("http://10.254.26.28:8888/front.gif"));
System.out.println("Decoding movie 2");
Movie movie2 = urlToMovie(new URL("http://10.254.26.28:8888/back.gif"));
System.out.println("Stream closed");
processFinish = true;
bothMovies = new Movie[]{movie1, movie2};
return bothMovies;
}
catch (IOException e) {
e.printStackTrace();
System.out.println("Shit got fucked");
}
return null;
}
@Override
protected void onPostExecute(Movie[] io) {
System.out.println("Post execute...");
super.onPostExecute(io);
System.out.println("Process finished");
}
}
protected void getTheStuffInTheCanvas(Canvas canvas){
long now = android.os.SystemClock.uptimeMillis();
Paint p = new Paint();
p.setAntiAlias(true);
if(movieStart == 0){
movieStart = now; }
int relTime;
// Drawing first image
relTime = (int)((now - movieStart) % bothMovies[0].duration());
bothMovies[0].setTime(relTime);
bothMovies[0].draw(canvas,0,0);
// Now second image
relTime = (int)((now - movieStart) % bothMovies[1].duration());
bothMovies[1].setTime(relTime);
bothMovies[1].draw(canvas,0,300);
this.invalidate();
}
protected Movie urlToMovie(URL url) {
System.out.println("Opening connection");
URLConnection conn = null;
Movie movie = null;
try {
conn = url.openConnection();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bis.mark(conn.getContentLength());
movie = Movie.decodeStream(bis);
bis.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Something wrong happened with the decoding");
}
return movie;
}
}
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Movie;
导入android.graphics.Paint;
导入android.os.AsyncTask;
导入android.util.AttributeSet;
导入android.view.view;
导入java.io.*;
导入java.net.URL;
导入java.net.URLConnection;
公共类视图扩展了视图{
长电影开始=0;
布尔processFinish=false;
电影[]两部电影;
布尔静态模式=假;
私有布尔完成=假;
上下文ctx;
公共视图(上下文){
超级(上下文);
新建GetGifFromNetwork().execute();
bothMovies=新电影[2];
ctx=上下文;
}
公共视图(上下文、属性集属性){
超级(上下文,attrs);
新建GetGifFromNetwork().execute();
bothMovies=新电影[2];
ctx=上下文;
}
公共视图(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
新建GetGifFromNetwork().execute();
bothMovies=新电影[2];
ctx=上下文;
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
System.out.println(“工程视图”);
if(静态模式){
System.out.println(“开始的东西,非常快”);
找到画布(画布);
}
否则{
正在(!完成){
if(processFinish){
System.out.println(“起始材料”);
找到画布(画布);
完成=正确;
静态模式=真;
}
}
}
}
类GetGifFromNetwork扩展了异步任务{
受保护的电影[]doInBackground(无效…对象){
输入流输入,in2;
System.out.println(“获取电影”);
试一试{
System.out.println(“解码电影1”);
Movie movie1=urlToMovie(新URL(“http://10.254.26.28:8888/front.gif"));
System.out.println(“解码电影2”);
Movie movie2=urlToMovie(新URL(“http://10.254.26.28:8888/back.gif"));
System.out.println(“流关闭”);
processFinish=true;
bothMovies=新电影[]{movie1,movie2};
返回两部影片;
}
捕获(IOE异常){
e、 printStackTrace();
系统输出打印(“狗屎被操”);
}
返回null;
}
@凌驾
受保护的void onPostExecute(电影[]io){
System.out.println(“后执行…”);
super.onPostExecute(io);
System.out.println(“过程完成”);
}
}
受保护的void gethestuffinthecanvas(画布){
long now=android.os.SystemClock.uptimeMillis();
油漆p=新油漆();
p、 setAntiAlias(真);
如果(movieStart==0){
movieStart=now;}
整数时间;
//绘制第一个图像
relTime=(int)((now-movieStart)%bothMovies[0].duration());
bothMovies[0]。设置时间(relTime);
bothMovies[0]。绘制(画布,0,0);
//现在是第二幅图
relTime=(int)((now-movieStart)%bothMovies[1].duration());
bothMovies[1]。设置时间(relTime);
bothMovies[1]。draw(canvas,0300);
这个。使无效();
}
受保护的电影URL(URL){
System.out.println(“打开连接”);
URLConnection conn=null;
Movie=null;
试一试{
conn=url.openConnection();
InputStream is=conn.getInputStream();
BufferedInputStream bis=新的BufferedInputStream(is);
双标记(conn.getContentLength());
电影=电影.解码流(bis);
二、关闭();
}捕获(IOE异常){
e、 printStackTrace();
System.out.println(“解码出了问题”);
}
回归电影;
}
}
编辑:以下是布局xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainLayout"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MainActivity"
/>
<view
class="com.dansd.coketruck.GifView"
android:layout_marginLeft="0dp" android:layout_gravity="center"
android:layout_width="wrap_content" android:layout_height="500dp"
android:id="@+id/GIFSingle">
</view>
</LinearLayout>
您可以将GIF加载到webview,这很简单,我不知道两个webview是否同时播放GIF,但它们应该是。我从webview开始。问题是,我希望以后能够控制GIF的帧,据我所知,WebView只显示您在浏览器中看到的GIF,对吗?能否提供layout.xml文件?也许这是错误的,在你看来,你没有衡量的方法。据我所知,WebView只能显示Gif,无法对其进行控制。我已将xml添加到问题的底部。:)