gpt4 book ai didi

java - 如何更改 LayerDrawable 中一张图像的大小

转载 作者:行者123 更新时间:2023-12-02 03:15:54 27 4
gpt4 key购买 nike

您好,我正在开发一个 Android 启动器,它的所有应用程序图标后面都有一个圆形背景。我正在使用 LayerDrawable 来使其工作,但是圆形图像比应用程序图标小,请参见此处:

enter image description here

所以我的问题是如何使圆圈图标比应用程序图标更大?

这是我的圆圈图标代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:color="#ccc" android:width="5dp" />
<solid android:color="#ffffff"/>
<size android:width="58dp" android:height="58dp"/>
</shape>
</item>
</selector>

这是我生成应用程序图标的类:

package Appname.widget;

import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.View;
import android.widget.ImageView;

import appame.R;
import appname.AppManager;
import appname.util.DragAction;
import appname.util.GoodDragShadowBuilder;
import appname.util.LauncherSettings;
import appname.util.Tool;


public class AppItemView extends View implements Drawable.Callback{


public Drawable getIcon() {


return icon;
}



public void setIcon(Drawable icon) {
this.icon = icon;
this.icon.setCallback(this);





}



@Override
public void refreshDrawableState() {
invalidateDrawable(icon);
super.refreshDrawableState();
}

@Override
public void invalidateDrawable(Drawable drawable) {
invalidate();
}

public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
}

public float getIconSize() {
return iconSize;
}

public void setIconSize(float iconSize) {
this.iconSize = iconSize;
}

private float iconSize;

private Drawable icon;
private String label;

public boolean isShortcut;

public Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Rect mTextBound = new Rect();

private boolean noLabel,vibrateWhenLongPress;

private float labelHeight;



public AppItemView(Context context) {
super(context);

init();
}

public AppItemView(Context context, AttributeSet attrs) {
super(context, attrs);

init();
}



private void init(){
setWillNotDraw(false);

labelHeight = Tool.convertDpToPixel(14,getContext());

textPaint.setTextSize(sp2px(getContext(),14));
textPaint.setColor(Color.DKGRAY);
}

public static int sp2px(Context context, float spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}

@Override
protected void onDraw(Canvas canvas) {



Drawable iconback = getResources().getDrawable(R.drawable.iconback);
LayerDrawable appicon = new LayerDrawable(new Drawable[]{iconback, icon});
appicon.setLayerGravity(0, Gravity.CENTER);
appicon.setLayerGravity(1, Gravity.CENTER);

if (label != null && !noLabel){
textPaint.getTextBounds(label,0,label.length(),mTextBound);
}

//The height should be the same as they have the same text size.
float mHeight = iconSize + (noLabel? 0 : labelHeight);
float heightPadding = (getHeight() - mHeight)/2f;

if (label != null && !noLabel) {
float x = (getWidth()-mTextBound.width())/2f;
if (x < 0)
x = 0;
canvas.drawText(label,x, getHeight() - heightPadding, textPaint);
}




if (appicon != null ) {
canvas.save();
canvas.translate((getWidth()-iconSize)/2,heightPadding);
appicon.setLayerWidth(1, (int) iconSize);
appicon.setLayerHeight(1, (int) iconSize);
appicon.setBounds(0,0,(int)iconSize,(int)iconSize);
appicon.draw(canvas);
canvas.restore();
}
}



public static class Builder{
AppItemView view;

public Builder(Context context){
view = new AppItemView(context);


float iconSize = Tool.convertDpToPixel(LauncherSettings.getInstance(view.getContext()).generalSettings.iconSize, view.getContext());
view.setIconSize(iconSize);


}

public AppItemView getView(){
return view;

}


public Builder setAppItem(AppManager.App app){
view.setIcon(app.icon);
view.setLabel(app.appName);



return this;
}

public Builder withOnClickLaunchApp(final AppManager.App app){
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Tool.createScaleInScaleOutAnim(view, new Runnable() {
@Override
public void run() {
Tool.startApp(view.getContext(), app);
}
});
}
});
return this;
}

public Builder withOnLongClickDrag(final AppManager.App app,final DragAction.Action action,@Nullable final OnLongClickListener eventAction){
withOnLongClickDrag(Desktop.Item.newAppItem(app),action,eventAction);
view.setScaleX(0.75f); // <- resized by scaling
view.setScaleY(0.75f);
return this;
}

public Builder withOnLongClickDrag(final Desktop.Item item, final DragAction.Action action, @Nullable final OnLongClickListener eventAction){

view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (view.vibrateWhenLongPress)
v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
Intent i = new Intent();
i.putExtra("mDragData", item);
ClipData data = ClipData.newIntent("mDragIntent", i);
v.startDrag(data, new GoodDragShadowBuilder(v), new DragAction(action), 0);
if (eventAction != null)
eventAction.onLongClick(v);
return true;

}
});
return this;
}

public Builder withOnTouchGetPosition(){
view.setOnTouchListener(Tool.getItemOnTouchListener());
return this;
}

public Builder setTextColor(@ColorInt int color){
view.textPaint.setColor(color);
return this;
}

public Builder setNoLabel(){
view.noLabel = true;
return this;
}

public Builder vibrateWhenLongPress(){
view.vibrateWhenLongPress = true;
return this;
}

public Builder setShortcutItem(final Intent intent){
view.isShortcut = true;
view.setScaleX(0.75f); // <- resized by scaling
view.setScaleY(0.75f);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Tool.createScaleInScaleOutAnim(view, new Runnable() {
@Override
public void run() {
view.getContext().startActivity(intent);



}
});
}
});
view.setScaleX(0.75f); // <- resized by scaling
view.setScaleY(0.75f);
view.setIcon(Tool.getIconFromID(view.getContext(),intent.getStringExtra("shortCutIconID")));
view.setLabel(intent.getStringExtra("shortCutName"));
return this;

}
}


}

请注 Intent 标是在 JAVA 中生成的,而不是 XML!

任何帮助都会很棒!

提前致谢:)

最佳答案

在创建 LayerDrawable 之前,您可以将图标包装在 InsetDrawable 中像这样:

Drawable iconWithPadding = new InsetDrawable(icon, paddingSize);
LayerDrawable appicon = new LayerDrawable(new Drawable[]{iconback, iconWithPadding});

这将有效地向图标应用填充。

关于java - 如何更改 LayerDrawable 中一张图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40309400/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com