Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make vector drawable in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 444
    Comment on it

    In the below example I have created vector drawable function. For this first I have created vectordrawable.xml, animvectordrawable.xml , anim/rotation.xml and path_morph.xml layout. In activity_main.xml layout I have added TextView and ImageView. And now In MainActivity I have used OnClickListener(). You can see the below example it will clearly describe you how to make vector drawable in android .

    Step(1)-Create vectordrawable.xml layout

    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:height="64dp"
        android:width="64dp"
        android:viewportHeight="600"
        android:viewportWidth="600">
        <group
            android:name="rotationGroup"
            android:pivotX="300.0"
            android:pivotY="300.0"
            android:rotation="45.0" >
            <path
                android:name="v"
                android:fillColor="#000000"
                android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
        </group>
    </vector>
    

    Step(2)-animvectordrawable.xml layout

    <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/vectordrawable" >
        <target
            android:name="rotationGroup"
            android:animation="@anim/rotation" />
        <target
            android:name="v"
            android:animation="@anim/path_morph" />
    </animated-vector>
    

    Step(3)-Create rotation.xml in anim folder

    <objectAnimator
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="6000"
        android:propertyName="rotation"
        android:valueFrom="0"
        android:valueTo="360" />
    

    Step(4)-Create path_morph.xml in anim folder

    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <objectAnimator
            android:duration="3000"
            android:propertyName="pathData"
            android:valueFrom="M300,70 l 0,-70 70,70 0,0   -70,70z"
            android:valueTo="M300,70 l 0,-70 70,0  0,140 -70,0 z"
            android:valueType="pathType" />
    </set>
    

    Step(5)-Create activity_main.xml layout-

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:autoLink="web"
            android:text="http://android-er.blogspot.com/"
            android:textStyle="bold" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
    
            <ImageView
                android:id="@+id/imageview1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/animvectordrawable"
                android:background="#D0D0D0"/>
            <ImageView
                android:id="@+id/imageview2"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:src="@drawable/animvectordrawable"
                android:background="#B0B0B0"/>
            <ImageView
                android:id="@+id/imageview3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/animvectordrawable"
                android:background="#909090"/>
        </LinearLayout>
    
    </LinearLayout>
    

    Step(6)-MainActivity-

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ImageView imageView1 = (ImageView)findViewById(R.id.imageview1);
            imageView1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Drawable drawable = ((ImageView) v).getDrawable();
                    ((Animatable) drawable).start();
                }
            });
    
            ImageView imageView2 = (ImageView)findViewById(R.id.imageview2);
            imageView2.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Drawable drawable = ((ImageView)v).getDrawable();
                    ((Animatable) drawable).start();
                }
            });
    
            ImageView imageView3 = (ImageView)findViewById(R.id.imageview3);
            imageView3.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Drawable drawable = ((ImageView)v).getDrawable();
                    ((Animatable) drawable).start();
                }
            });
        }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: