gpt4 book ai didi

android - 来自 URL 的 StateListDrawable 可绘制对象

转载 作者:行者123 更新时间:2023-11-30 01:50:03 24 4
gpt4 key购买 nike

我正在尝试制作一个带有 StateListDrawable 但带有 2 个远程图像的按钮。

我正在尝试这样:

package com.mylisabox.common.helpers;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.AsyncTask;

import java.io.InputStream;
import java.net.URL;

/**
* Created by jaumard on 16/10/2015.
*/
public class RemoteStateListDrawable extends StateListDrawable
{
Drawable drawableOn;
Drawable drawableOff;

public RemoteStateListDrawable(String urlOn, String urlOff)
{
loadImageAsDrawable(urlOff, false);
loadImageAsDrawable(urlOn, true);
}

@Override
protected boolean onStateChange(int[] stateSet)
{
return super.onStateChange(stateSet);
}

@Override
public boolean isStateful()
{
return true;
}

public void loadImageAsDrawable(final String url, final boolean isOnState)
{
new AsyncTask<Void, Void, Drawable>()
{

@Override
protected Drawable doInBackground(Void... params)
{
try
{
// open the stream
InputStream is = new URL(url).openStream();
String imageName = "src";
Drawable draw = Drawable.createFromStream(is, imageName);
return draw;
}
catch (Exception e)
{
// something went wrong
return null;
}
}

@Override
protected void onPostExecute(Drawable drawable)
{
super.onPostExecute(drawable);
if (isOnState)
{
drawableOn = drawable;
addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_selected}, drawableOn);

}
else
{
drawableOff = drawable;
addState(new int[]{}, drawableOff);
}
}
}.execute();

}
}

但它不起作用...如果我单击按钮,会显示关闭图像,但不会显示打开图像。

最佳答案

  1. android.R.attr.state_pressed,android.R.attr.state_selected 是按下和选中的意思,去掉选中再试2.尝试交换可绘制的关闭和打开3.尝试使用不同的图像名称

     String imageName = "src";

关于android - 来自 URL 的 StateListDrawable 可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33185162/

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