我正在尝试在android studio上制作气泡选择器动画,但出现此错误:
错误:不兼容的类型:int无法转换为Drawable
这表明该行中存在错误
PickerItem item = new PickerItem(name[i],colors[i],Color.WHITE,getResources().getDrawable(images[i]));
当我将鼠标悬停在那条线上时,它会显示类似这样的内容
这是Java代码:
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.igalata.bubblepicker.BubblePickerListener;
import com.igalata.bubblepicker.model.PickerItem;
import com.igalata.bubblepicker.rendering.BubblePicker;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
BubblePicker bubblePicker;
int i;
String[] name={
"anime1","anime2","anime3","anime4","anime5","anime6"
};
int[] images={
R.drawable.x1,
R.drawable.x2,
R.drawable.x3,
R.drawable.x4,
R.drawable.x5,
R.drawable.x6
};
int[] colors={
Color.parseColor("#1A237E"),
Color.parseColor("#6200EA"),
Color.parseColor("#004D40"),
Color.parseColor("#1A237E"),
Color.parseColor("#880E4F"),
Color.parseColor("#B71C1C")
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bubblePicker = (BubblePicker) findViewById(R.id.bubble);
ArrayList<PickerItem> listItems = new ArrayList<>();
for(int i = 0; i<name.length; i++)
{
//Drawable drawable= getResources().getDrawable(images[i]);
PickerItem item = new PickerItem(name[i],colors[i],Color.WHITE,getResources().getDrawable(images[i]));
listItems.add(item);
}
bubblePicker.setItems(listItems);
bubblePicker.setListener(new BubblePickerListener() {
@Override
public void onBubbleSelected(PickerItem pickerItem) {
Toast.makeText(getApplicationContext(),""+pickerItem.getTitle()+"selected",Toast.LENGTH_SHORT).show();
}
@Override
public void onBubbleDeselected(PickerItem pickerItem) {
Toast.makeText(getApplicationContext(),""+pickerItem.getTitle()+"Deselected",Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onResume(){
super.onResume();
bubblePicker.onResume();
}
@Override
protected void onPause(){
super.onPause();
bubblePicker.onPause();
}
}
答案 0 :(得分:1)
我猜您为构造函数使用了错误的参数顺序,应该是:
PickerItem item = new PickerItem(name[i], getResources().getDrawable(images[i]), true/false, Color.WHITE);
或者类似的东西,不知道您的要求是什么。
答案 1 :(得分:1)
我认为您感到困惑。 PickerItem 构造函数按以下特定顺序需要这四个参数:
因此,它可能看起来像这样:
PickerItem item = new PickerItem(name[i], context.getDrawable(images[i]), hasIconOnTop, colors[i]);
其中hasIconOnTop
是一个布尔值值。
答案 2 :(得分:0)
PickerItem构造函数具有以下签名
new PickerItem (String, Drawable, boolean, Integer)
,您将其用作:
new PickerItem (String, int, int, Drawable)
您应该尝试使用:
PickerItem item = new PickerItem(name[i],getResources().getDrawable(images[i]),true, colors[i]);