Search in sources :

Example 6 with VectorDrawable

use of android.graphics.drawable.VectorDrawable in project platform_frameworks_base by android.

the class VectorDrawablePerformance method create.

public static VectorDrawable create(Resources resources, int rid) {
    try {
        final XmlPullParser parser = resources.getXml(rid);
        final AttributeSet attrs = Xml.asAttributeSet(parser);
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
        // Empty loop
        }
        if (type != XmlPullParser.START_TAG) {
            throw new XmlPullParserException("No start tag found");
        }
        final VectorDrawable drawable = new VectorDrawable();
        drawable.inflate(resources, parser, attrs);
        return drawable;
    } catch (XmlPullParserException e) {
        Log.e(LOGCAT, "parser error", e);
    } catch (IOException e) {
        Log.e(LOGCAT, "parser error", e);
    }
    return null;
}
Also used : AttributeSet(android.util.AttributeSet) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) VectorDrawable(android.graphics.drawable.VectorDrawable)

Example 7 with VectorDrawable

use of android.graphics.drawable.VectorDrawable in project platform_frameworks_base by android.

the class VectorDrawablePerformance method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ScrollView scrollView = new ScrollView(this);
    GridLayout container = new GridLayout(this);
    scrollView.addView(container);
    container.setColumnCount(4);
    Resources res = this.getResources();
    container.setBackgroundColor(0xFF888888);
    VectorDrawable[] d = new VectorDrawable[icon.length];
    long time = android.os.SystemClock.elapsedRealtimeNanos();
    for (int i = 0; i < icon.length; i++) {
        d[i] = create(res, icon[i]);
    }
    time = android.os.SystemClock.elapsedRealtimeNanos() - time;
    TextView t = new TextView(this);
    DecimalFormat df = new DecimalFormat("#.##");
    t.setText("avgL=" + df.format(time / (icon.length * 1000000.)) + " ms");
    container.addView(t);
    time = android.os.SystemClock.elapsedRealtimeNanos();
    for (int i = 0; i < icon.length; i++) {
        Button button = new Button(this);
        button.setWidth(200);
        button.setBackgroundResource(icon[i]);
        container.addView(button);
    }
    setContentView(scrollView);
    time = android.os.SystemClock.elapsedRealtimeNanos() - time;
    t = new TextView(this);
    t.setText("avgS=" + df.format(time / (icon.length * 1000000.)) + " ms");
    container.addView(t);
}
Also used : GridLayout(android.widget.GridLayout) ScrollView(android.widget.ScrollView) Button(android.widget.Button) DecimalFormat(java.text.DecimalFormat) TextView(android.widget.TextView) Resources(android.content.res.Resources) VectorDrawable(android.graphics.drawable.VectorDrawable)

Example 8 with VectorDrawable

use of android.graphics.drawable.VectorDrawable in project android_frameworks_base by crdroidandroid.

the class ImageHelper method getColoredDrawable.

public static Drawable getColoredDrawable(Drawable d, int color) {
    if (d == null) {
        return null;
    }
    if (d instanceof VectorDrawable) {
        d.setTint(color);
        return d;
    }
    Bitmap colorBitmap = ((BitmapDrawable) d).getBitmap();
    Bitmap grayscaleBitmap = toGrayscale(colorBitmap);
    Paint pp = new Paint();
    pp.setAntiAlias(true);
    PorterDuffColorFilter frontFilter = new PorterDuffColorFilter(color, Mode.MULTIPLY);
    pp.setColorFilter(frontFilter);
    Canvas cc = new Canvas(grayscaleBitmap);
    final Rect rect = new Rect(0, 0, grayscaleBitmap.getWidth(), grayscaleBitmap.getHeight());
    cc.drawBitmap(grayscaleBitmap, rect, rect, pp);
    return new BitmapDrawable(grayscaleBitmap);
}
Also used : Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) Canvas(android.graphics.Canvas) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Paint(android.graphics.Paint) VectorDrawable(android.graphics.drawable.VectorDrawable)

Example 9 with VectorDrawable

use of android.graphics.drawable.VectorDrawable in project android_frameworks_base by crdroidandroid.

the class ImageHelper method resize.

public static Drawable resize(Context context, Drawable image, int size) {
    if (image == null || context == null) {
        return null;
    }
    if (image instanceof VectorDrawable) {
        return image;
    } else {
        int newSize = Converter.dpToPx(context, size);
        Bitmap bitmap = ((BitmapDrawable) image).getBitmap();
        Bitmap scaledBitmap = Bitmap.createBitmap(newSize, newSize, Config.ARGB_8888);
        float ratioX = newSize / (float) bitmap.getWidth();
        float ratioY = newSize / (float) bitmap.getHeight();
        float middleX = newSize / 2.0f;
        float middleY = newSize / 2.0f;
        final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        paint.setAntiAlias(true);
        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
        Canvas canvas = new Canvas(scaledBitmap);
        canvas.setMatrix(scaleMatrix);
        canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, paint);
        return new BitmapDrawable(context.getResources(), scaledBitmap);
    }
}
Also used : Bitmap(android.graphics.Bitmap) ColorMatrix(android.graphics.ColorMatrix) Matrix(android.graphics.Matrix) Canvas(android.graphics.Canvas) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Paint(android.graphics.Paint) VectorDrawable(android.graphics.drawable.VectorDrawable) Paint(android.graphics.Paint)

Example 10 with VectorDrawable

use of android.graphics.drawable.VectorDrawable in project android_frameworks_base by crdroidandroid.

the class ColorHelper method getColoredDrawable.

public static Drawable getColoredDrawable(Drawable d, int color) {
    if (d instanceof VectorDrawable) {
        d.setTint(color);
        return d;
    }
    Bitmap colorBitmap = ((BitmapDrawable) d).getBitmap();
    Bitmap grayscaleBitmap = toGrayscale(colorBitmap);
    Paint pp = new Paint();
    PorterDuffColorFilter frontFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY);
    pp.setColorFilter(frontFilter);
    Canvas cc = new Canvas(grayscaleBitmap);
    cc.drawBitmap(grayscaleBitmap, 0, 0, pp);
    return new BitmapDrawable(grayscaleBitmap);
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Paint(android.graphics.Paint) VectorDrawable(android.graphics.drawable.VectorDrawable)

Aggregations

VectorDrawable (android.graphics.drawable.VectorDrawable)33 BitmapDrawable (android.graphics.drawable.BitmapDrawable)19 Bitmap (android.graphics.Bitmap)13 Canvas (android.graphics.Canvas)13 Paint (android.graphics.Paint)9 Button (android.widget.Button)8 GridLayout (android.widget.GridLayout)8 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)6 AnimationDrawable (android.graphics.drawable.AnimationDrawable)5 Resources (android.content.res.Resources)4 Rect (android.graphics.Rect)4 VectorDrawableCompat (android.support.graphics.drawable.VectorDrawableCompat)4 AttributeSet (android.util.AttributeSet)4 View (android.view.View)4 ViewGroup (android.view.ViewGroup)4 CheckBox (android.widget.CheckBox)4 CompoundButton (android.widget.CompoundButton)4 OnCheckedChangeListener (android.widget.CompoundButton.OnCheckedChangeListener)4 ScrollView (android.widget.ScrollView)4 TextView (android.widget.TextView)4