获得像gmail应用程序的方形图像

时间:2013-12-30 06:14:42

标签: android image

我想用gmail app等字母显示图像,如下图所示。

enter image description here

是否所有这些图像都保存在可绘制文件夹中,或者它们被绘制为square shapes,然后将字母绘制到它们中?以下是我到目前为止动态做的事情。我只是一个方形。有人可以建议在gmail应用程序中实现的方式吗?

GradientDrawable gd = new GradientDrawable();
            gd.mutate();
            gd.setColor(getResources().getColor(gColors[i]));
button.setBackgroundDrawable(gd);

3 个答案:

答案 0 :(得分:22)

更新2:

我修复了一些错误,并将代码作为开源库发布在:https://github.com/amulyakhare/TextDrawable。它还包括您可能想要查看的一些其他功能。

sample screenshot


旧答案:

我建议您使用以下课程CharacterDrawable(只需复制粘贴):

public class CharacterDrawable extends ColorDrawable {

    private final char character;
    private final Paint textPaint;
    private final Paint borderPaint;
    private static final int STROKE_WIDTH = 10;
    private static final float SHADE_FACTOR = 0.9f;

    public CharacterDrawable(char character, int color) {
        super(color);
        this.character = character;
        this.textPaint = new Paint();
        this.borderPaint = new Paint();

        // text paint settings
        textPaint.setColor(Color.WHITE);
        textPaint.setAntiAlias(true);
        textPaint.setFakeBoldText(true);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextAlign(Paint.Align.CENTER);

        // border paint settings
        borderPaint.setColor(getDarkerShade(color));
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(STROKE_WIDTH);
    }

    private int getDarkerShade(int color) {
        return Color.rgb((int)(SHADE_FACTOR * Color.red(color)),
                         (int)(SHADE_FACTOR * Color.green(color)),
                         (int)(SHADE_FACTOR * Color.blue(color)));
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        // draw border
        canvas.drawRect(getBounds(), borderPaint);

        // draw text
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        textPaint.setTextSize(height / 2);
        canvas.drawText(String.valueOf(character), width/2, height/2 - ((textPaint.descent() + textPaint.ascent()) / 2) , textPaint);
    }

    @Override
    public void setAlpha(int alpha) {
        textPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        textPaint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

然后使用这个很简单:new CharacterDrawable('A', 0xFF805781);通过传递字符和颜色值(示例Color.RED或其他颜色的十六进制0xFF805781):

ImageView imageView = (ImageView) findViewById(R.id.imageView);
CharacterDrawable drawable = new CharacterDrawable('A', 0xFF805781);
imageView.setImageDrawable(drawable); 

或根据您的问题:

CharacterDrawable drawable = new CharacterDrawable('A', 0xFF805781);    
button.setBackgroundDrawable(drawable);

drawable将缩放以适合ImageView的大小。结果将是:

enter image description here

  

更新:更新了添加深色阴影边框的代码(根据填充颜色自动选择暗色调)。

1)根据您对边界苛刻的需求更改STROKE_WIDTH的值。

2)更改边框黑暗的SHADE_FACTOR值。如果SHADE_FACTOR很小(例如0.2f),则边框会变暗,反之亦然。

注意:您可以轻松改变角色的大小和字体

答案 1 :(得分:2)

简单的是你使用线性布局并设置背景颜色并在根布局中设置TectView。结束了。

你应该使用ColorCode Intesed的图像,这些图像与在UI线程上加载时使用图像相比是好事。

  <LinearLayout
            android:id="@+id/get_more"
            android:layout_width="70dp" // this root layout will set your square 
            android:layout_height="70dp"
            android:background="#654321" // set background color of square 
            android:orientation="horizontal" >


            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:textSize="24sp"
                android:text="C"
                android:background="#ffffff" // Text Color , set as White
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </LinearLayout>

答案 2 :(得分:0)

我稍微调整了一下代码......,即使屏幕尺寸不同,它也可以随时工作。诀窍是以像素为单位获取ImageView画布大小(有时候密度取决于各种设备)

package net.mypapit.android.ui;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.ColorDrawable;


public class CharacterDrawable extends ColorDrawable {

    private final char character;
    private final Paint textPaint;
    private final Paint borderPaint;
    private static final int STROKE_WIDTH = 10;
    private static final float SHADE_FACTOR = 0.9f;
    private int mwidth, mheight;

    public CharacterDrawable(char character, int color, int width, int height) {
        super(color);
        this.character = character;
        this.textPaint = new Paint();
        this.borderPaint = new Paint();
        this.mwidth = width;
        this.mheight = height;

        // text paint settings 
        textPaint.setColor(Color.WHITE);
        textPaint.setAntiAlias(true);
        textPaint.setFakeBoldText(true);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextAlign(Paint.Align.CENTER);

        // border paint settings 
        borderPaint.setColor(getDarkerShade(color));
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(STROKE_WIDTH);
    } 

    private int getDarkerShade(int color) {
        return Color.rgb((int)(SHADE_FACTOR * Color.red(color)),
                         (int)(SHADE_FACTOR * Color.green(color)),
                         (int)(SHADE_FACTOR * Color.blue(color)));
    } 

    public void draw(Canvas canvas) {
        super.draw(canvas);

        // draw border 
        canvas.drawRect(getBounds(), borderPaint);

        // draw text 
        int width = this.mwidth;
        int height = this.mheight;
        textPaint.setTextSize(height / 2);
        canvas.drawText(String.valueOf(character), width/2, height/2 - ((textPaint.descent() + textPaint.ascent()) / 2) , textPaint);
    } 

    public void setAlpha(int alpha) {
        textPaint.setAlpha(alpha);
    } 

    public void setColorFilter(ColorFilter cf) {
        textPaint.setColorFilter(cf);
    } 

    public int getOpacity() { 
        return PixelFormat.TRANSLUCENT;
    } 
} 

然后,请参阅最初的Amulya Khare答案:

ImageView imageView = (ImageView) findViewById(R.id.imageView);
CharacterDrawable drawable = new CharacterDrawable('A', 0xFF805781,imageView.getWidth(),imageView.getHeight());
imageView.setImageDrawable(drawable); 

现在它应该适用于不同的屏幕密度=)