通过微调器选择以编程方式更改WebView Gif

时间:2014-10-28 10:03:19

标签: android view android-spinner animated-gif

我将一个WebView放在一个片段中以显示一个gif动画。这非常有效。这是WebView类:

public class GifWebView extends View {
    private Movie mMovie;
    InputStream is;
    long moviestart;

    public GifWebView(Context context, String gifName) {
    	
    	super(context);
    	Paint p = new Paint();
    	p.setAntiAlias(true);
    	setLayerType(LAYER_TYPE_SOFTWARE, p);
    	
    	if(gifName.startsWith("acqua")){

    		is=context.getResources().openRawResource(R.drawable.water);

    	} else if(gifName.startsWith("aria")) {
    		
    		is=context.getResources().openRawResource(R.drawable.air);
    	} 
    	
    	mMovie=Movie.decodeStream(is);
    }

    @Override
	protected void onDraw(Canvas canvas) {

		canvas.drawColor(Color.WHITE);
		super.onDraw(canvas);
		long now=android.os.SystemClock.uptimeMillis();
		//System.out.println("now="+now);
		if (moviestart == 0) { // first time
			
			moviestart = now;
		}
		//System.out.println("\tmoviestart="+moviestart);
		int relTime = (int)((now - moviestart) % mMovie.duration()) ;
		//System.out.println("time="+relTime+"\treltime="+movie.duration());
		mMovie.setTime(relTime);
		mMovie.draw(canvas,(float)this.getWidth()/(float) mMovie.width(),(float)this.getHeight()/(float)   mMovie.height());
		this.invalidate();
	}
}

当我想停止gif动画并通过我的片段中的微调器选择用另一个gif更改它时出现问题:

mySpinner.setOnItemSelectedListener(new OnItemSelectedListener()  {
	        
      @Override 
	  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {    
	    
	      int posit = parent.getSelectedItemPosition();
	    			
	      pertSpinner.setPopupBackgroundResource(R.color.white);
	      pertSpinner.setValConfirmed(true);
	               
	      switch (posit) {
	     
              case 0 :
	    		   			
	    	  toplayout.removeAllViewsInLayout();
	          view = new GifWebView(getActivity(), "acqua");
	    	  toplayout.addView(view);
	    		   			
	          break;
	    		   			
	          case 1 :

	    	  toplayout.removeAllViewsInLayout();
	    	  view = new GifWebView(getActivity(), "aria");
	    	  toplayout.addView(view);
	    		   		
	    	  break;
         }

	 }

     @Override
	 public void onNothingSelected(AdapterView<?> parent) {
	      // Another interface callback
	        	
	 }
});

这是我的XML布局,其中包含了包含gif:

的toplayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.example.firacalc"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:id="@+id/fragment_container" >
  
    ... some buttons and the spinner...

    <LinearLayout
        android:id="@+id/toplayout"
        android:layout_width="116dp"
        android:layout_height="116dp"
        android:layout_alignTop="@+id/btnInfo"
        android:layout_marginLeft="27dp"
        android:layout_toRightOf="@+id/btnInfo"
        android:layout_alignRight="@+id/pert_spinner"
        android:orientation="horizontal" >
   </LinearLayout>
  
</RelativeLayout>

使用此代码,应用程序不会更改默认GIF。 有没有一种方法通过微调器以编程方式设置新的GIF? 任何帮助将是apreciated !!

1 个答案:

答案 0 :(得分:0)

解决:

&#13;
&#13;
switch (posit) {

	   case 0 :

	   toplayout.removeAllViewsInLayout();
	   view = new GifWebView(getActivity(), "aria");
	   toplayout.addView(view);
	   toplayout.setVisibility(View.VISIBLE);
	    		   		
	   break;
	    		   			
	   case 1 :

	   toplayout.removeAllViewsInLayout();
	   view = new GifWebView(getActivity(), "acqua");
	   toplayout.addView(view);
	    		   			
	   break;

}
&#13;
&#13;
&#13;