Click here to Skip to main content
15,896,259 members
Articles / Mobile Apps / Android

Android ImageView and Drawable with SVG Support

Rate me:
Please Sign up or sign in to vote.
4.75/5 (11 votes)
14 Dec 2010CPOL3 min read 155.5K   5.7K   33  
This article introduces custom classes SvgImageView and SvgDrawable which allow to use SVG image just like any other image.
package com.ImageViewSvg;

/**
 * 
 * @author Pavel.B.Chernov (based on ideas of kushnarev)
 *
 */

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
    }

    /*
     * Walks through the objects tree
     * When meets ImageView - sets its width and height 
     */
    private void SetChildsWidthHeight(ViewGroup view, int width, int height) {
    	int i;
    	for (i = 0; i < view.getChildCount(); ++i) {
    		View child = view.getChildAt(i);
    		if (child instanceof ViewGroup) {
    			SetChildsWidthHeight((ViewGroup)child, width, height);
    		}
    		else if (child instanceof ImageView) {
    			ImageView img = (ImageView) child;
    			img.setLayoutParams(new TableRow.LayoutParams(width, height));
    		}
    	}
    }

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
    	super.onWindowFocusChanged(hasFocus);

        if (hasFocus) {
    		TableLayout table = (TableLayout) findViewById(R.id.TableView);
        	if (table == null) {
        		return;
            }

        	// Set uniform width and height for all ImageViews in table
        	SetChildsWidthHeight(table, table.getWidth() / 3, table.getHeight() / 2);
        }
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions