在扩展View类以创建同时播放两个GIF的视图时,我正在努力。到目前为止,我已成功从资产中加载两个Gif,但我需要应用程序从几个URL加载它们。这就是应用程序现在显示的内容,两个GIF没有移动,实际上只显示了它们中的一小部分。
这是视图的代码:
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;
}
}
编辑:这是布局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>
答案 0 :(得分:0)
你可以将GIF加载到webview,这很简单,我不知道两个webview是否会同时播放GIF,但它们应该。