你好我正在开发一个Android启动器,我希望它在每个应用程序图标后面都有一个圆圈形状我通过java代码NOT XML引用形状!所以我用这个代码作为我的形状:
<?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="2dp" />
<solid android:color="#ffffff"/>
<size android:width="100dp" android:height="100dp"/>
</shape>
</item>
</selector>
这是我的课程,为启动器制作应用程序:
package appname.widget;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.HapticFeedbackConstants;
import android.view.View;
import android.widget.ImageView;
import appname.R;
import appname.util.AppManager;
import appname.util.DragAction;
import appname.util.GoodDragShadowBuilder;
import appname.util.LauncherSettings;
import appname.util.Tool;
import static android.R.attr.button;
import static android.R.attr.width;
import static com.bennyv5.materialpreffragment.R.attr.height;
/**
* Created by BennyKok on 10/23/2016.
*/
public class AppItemView extends View implements Drawable.Callback{
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon, boolean isStatic) {
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) {
super.onDraw(canvas);
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 (icon != null){
canvas.save();
canvas.translate((getWidth()-iconSize)/2,heightPadding);
icon.setBounds(0,0,(int)iconSize,(int)iconSize);
icon.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,true);
view.setLabel(app.appName);
view.setBackgroundResource(R.drawable.iconbackbg);
view.setScaleX(0.75f); // <- resized by scaling
view.setScaleY(0.75f);
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.setIcon(Tool.getIconFromID(view.getContext(),intent.getStringExtra("shortCutIconID")),true);
view.setLabel(intent.getStringExtra("shortCutName"));
return this;
}
}
}
编辑我尝试过建议,但圆圈并没有显示出更新后的绘制方法:
@Override
protected void onDraw(Canvas canvas) {
//Load your oval shape
Drawable background = getResources().getDrawable(R.drawable.iconbackbg, getContext().getTheme());
//create a LayerDrawable that combines the oval shape with the icon
Drawable[] layers = {background, icon};
LayerDrawable layerDrawable = new LayerDrawable(layers);
//set the gravity of both drawables to center
layerDrawable.setLayerGravity(0, Gravity.CENTER);
layerDrawable.setLayerGravity(1, Gravity.CENTER);
super.onDraw(canvas);
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 (layerDrawable != null){
canvas.save();
canvas.translate((getWidth()-iconSize)/2,heightPadding);
layerDrawable.setLayerWidth(1, (int) iconSize);
layerDrawable.setLayerHeight(1, (int) iconSize);
layerDrawable.draw(canvas);
canvas.restore();
}
if (icon != null){
canvas.save();
canvas.translate((getWidth()-iconSize)/2,heightPadding);
icon.setBounds(0,0,(int)iconSize,(int)iconSize);
icon.draw(canvas);
canvas.restore();
}
}
圆圈形状正在显示其刚刚调整大小的非常时髦的截图,它看起来链接在nexus 7平板电脑上:
任何帮助都会很棒!
提前致谢:)
答案 0 :(得分:0)
我猜R.drawable.iconbackbg
是ShapeDrawable的参考?
如果是这样,将其设置为View的背景会使其以View的边界绘制。因此,如果View的宽度和高度不相等,它将绘制一个椭圆作为背景而不是圆形(因此形状的名称为“椭圆形”)。
如果要将其绘制为圆形,则需要以某种方式覆盖drawables边界。当使用平台drawable并将它们设置为视图的背景时,这是不可能的,因为视图将在draw()期间覆盖drawables边界。
一种可能的解决方案是将应用程序的图标与椭圆形ShapeDrawable一起包装在LayerDrawable中(LayerDrawable基本上是一个彼此重叠绘制的drawable列表):
private void init(){
//... do your initilizations
//Load your oval shape
background = getResources().getDrawable(R.drawable.iconbackbg, getContext().getTheme());
}
public void setIcon(Drawable icon){
//... set the icon, be sure to remove callbacks to the previously set icon!
//create a LayerDrawable that combines the oval shape with the icon
LayerDrawable layerDrawable = new LayerDrawable(background, icon);
//set the gravity of both drawables to center
layerDrawable.setLayerGravity(0, Gravity.CENTER);
layerDrawable.setLayerGravity(1, Gravity.CENTER);
}
然后在你的onDraw()中执行:
if (layerDrawable != null){
canvas.save();
canvas.translate((getWidth()-iconSize)/2,heightPadding);
layerDrawable.setLayerWidth(1, (int) iconSize);
layerDrawable.setLayerHeight(1, (int) iconSize);
layerDrawable.draw(canvas);
canvas.restore();
}