2017-03-06 66 views
0

我发现了很多关于imageview中图像更改与其他图像的问题,但我有不同的问题。如何在图像视图中更改svg图像的xml源

我必须在xml格式的可绘制文件夹中有两个svg图像。 我想在imageview点击图像之间切换。就像如果图像a.xml是那里然后点击它应该显示图像b.xml,要做到这一点,我必须获取imageview的当前xml图像源并将其设置为另一个。

如何做到这一点?

MainActivity.java:

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ImageView; 

public class MainActivity extends AppCompatActivity { 
ImageView iv; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     iv = (ImageView) findViewById(R.id.imageView); 

     iv.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       if(v == iv){ 

        if(/* How to check current current source of imagevie here */ ){ 
/* How to set new xml svg source to imagevie here */ 
        } 

       } 

      } 
     }); 


    } 
} 

XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#fff" 
    tools:context="com.abcd.MainActivity"> 


    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     app:srcCompat="@android:drawable/alert_dark_frame" 
     android:src="@drawable/a" 
     android:layout_centerHorizontal="true" 

     android:id="@+id/imageView" 
     android:layout_alignParentTop="true" /> 


</FrameLayout> 

drawable folder containing two svg sources

回答

0

想通了部分解决方案,因为我可以设置新的SVG源的ImageView,但仍然可以” t电流源:

if(v == iv){ 

       int k = 10; 
       switch (count) { 
        case 1 : 

         iv.setImageResource(R.drawable.a); 
         k = 0 ; 
         break; 
        case 0 : 
         iv.setImageResource(R.drawable.b); 
         k=1 ; 
         break; 
       } 
       count = k ; 

      }