Search code examples
androidviewandroid-spinneranimated-gif

Change a WebView Gif programmatically via spinner selection


I put a WebView in a fragment in order to show a gif animation. That works perfectly. Here is the WebView class:

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();
	}
}

Problems come when I want to stop the gif animation and change it with another gif by spinner selection in my fragment:

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
	        	
	 }
});

And this is my XML layout with the toplayout containing the gif:

<?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>

With this code the App doesn't change the default GIF. Is there a method to set a new GIF programmatically via the spinner? Any help will be apreciated!!


Solution

  • SOLVED:

    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;
    
    }