在 Android 中调整可绘制对象的大小
我正在为进度对话框(pbarDialog
)设置一个可绘制对象,但我的问题是我想每次调整可绘制对象的大小,但不知道如何操作。
这是一些代码:
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
// some more code
case UPDATE_PBAR:
pbarDialog.setIcon(mAppIcon);
pbarDialog.setMessage(mPbarMsg);
pbarDialog.incrementProgressBy(mIncrement+1);
break;
}
}
};
pbarDialog.show();
Thread myThread = new Thread(new Runnable() {
public void run() {
// some code
for (int i = 0; i < mApps.size(); i++) {
mAppIcon = mAdapter.getIcons().get(mApps.get(i).getPackageName());
// need to resize drawable here
progressHandler.sendEmptyMessage(UPDATE_PBAR);
}
handler.sendEmptyMessage(DISMISS_PBAR);
}
});
myThread.start();
I am setting a drawable for a progress dialog (pbarDialog
) but my issue is I want to resize the drawable each time but can't figure out how.
Here is some code:
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
// some more code
case UPDATE_PBAR:
pbarDialog.setIcon(mAppIcon);
pbarDialog.setMessage(mPbarMsg);
pbarDialog.incrementProgressBy(mIncrement+1);
break;
}
}
};
pbarDialog.show();
Thread myThread = new Thread(new Runnable() {
public void run() {
// some code
for (int i = 0; i < mApps.size(); i++) {
mAppIcon = mAdapter.getIcons().get(mApps.get(i).getPackageName());
// need to resize drawable here
progressHandler.sendEmptyMessage(UPDATE_PBAR);
}
handler.sendEmptyMessage(DISMISS_PBAR);
}
});
myThread.start();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
以下对我有用:
The following worked for me:
这就是我的结局,部分感谢萨阿德的回答:
Here's where I ended up, thanks in part to Saad's answer:
对于调整大小,这很好而且很短(上面的代码对我不起作用),找到 此处:
For the resizing, this is nice and short (the code above wasn't working for me), found here:
这是上述答案的组合作为 Kotlin 扩展
用法:
或
Here is a combination of the above answers as a Kotlin extension
Usage:
or
也许我的解决方案没有完全涵盖这个问题,但我需要类似“CustomDrawable”的东西。
换句话说,我想在圆形前面设置一个徽标。因此,我创建了一个带有背景(只是一个彩色圆圈)的 FrameLayout,并在这个圆形形状的前面显示了徽标。
要调整徽标大小,我通过缩放缩小徽标 - 这是一些代码:
这是 FrameLayout,它显示 ListView 行内的图标:
将此解决方案视为选项/想法。
Maybe my solution covers the question not completely, but I needed something like a "CustomDrawable".
In other words, I want to set a logo in front of a circle shape. So I created a FrameLayout with a background (just a colored circle) and in front of this round shape I show the logo.
To resize the logo I shrink the logo by scaling - here is some code:
Here is the FrameLayout which shows the icon inside ListView's row:
See this solution as an option / idea.
如果源 Drawable 不是 BitmapDrawable 的实例,则投票最多的答案将不起作用,这可能是使用矢量、颜色可绘制等的情况...
最合适的解决方案可能是将 Drawable 绘制到具有设置位图的 Canvas 中,如下所示:
奖励:要计算要应用的比例(例如,将 Drawable 缩放到视图时):
注意:始终在工作线程中执行此操作!
The most voted answer wont work if the source Drawable is not instanceof BitmapDrawable which can be the case of using vector, color drawables, etc...
The most appropriate solution could be to draw the Drawable into a Canvas with set bitmap, as following:
BONUS: To calculate the scale to be applied (e.g. when scaling the Drawable to a view):
NOTE: ALWAYS do this in a worker thread!
Kotlin 方式
最终对我有用的是这个简单的解决方案
Kotlin way
What it ended up working for me was this simple solution
对于仍在解决这个问题的任何人来说,请记住,这里评价最高的答案并不是正确的选择。如果您拥有的 Drawable 不是从 BitmapDrawable 扩展的,则将任何 Drawable 转换为 BitmapDrawable 是行不通的,您将得到一个 ClassCastException 。
这是我最终用于调整可绘制对象大小的代码,它适用于各种可绘制对象:
Kotlin
Java
For anyone still struggling with this issue, have in mind that the top rated answers here are not the way to go. Casting any Drawable to BitmapDrawable is not gonna work if the Drawable you have does not extend from BitmapDrawable, you'll get a ClassCastException instead.
This is the code I ended up using for resizing a Drawable which works for every kind of Drawable:
Kotlin
Java