Jan 19, 2014

Copy drawable between ImageViews

Example to get drawable from one ImageView, then setImageDrawable to another ImageView.

Copy drawable between ImageViews
Copy drawable between ImageViews

package com.example.androidcopyimage;

import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.drawable.Drawable;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  ImageView source = (ImageView)findViewById(R.id.source);
  ImageView target = (ImageView)findViewById(R.id.target);
  
  Drawable drawable = source.getDrawable();
  target.setImageDrawable(drawable);
 }
}

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />
    <ImageView
        android:id="@+id/source"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
    <ImageView
        android:id="@+id/target"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
        

</LinearLayout>

No comments:

Post a Comment

Infolinks In Text Ads