我正在使用 XML 可绘制对象并使用图层动态地将它们叠加在彼此之上。每次用户点击一个按钮时,它都会覆盖另一个图像。问题是,一旦我有五层或十层这种特定的 XML 可绘制对象,那么 UI 就想锁定。这是可绘制的:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="247.5"
android:toDegrees="247.5"
android:pivotX="50%"
android:pivotY="50%">
<shape android:shape="oval">
<size
android:width="800dp"
android:height="800dp"/>
<stroke
android:dashWidth="235.6dp"
android:dashGap="5000.01dp"
android:width="200dp"
android:color="#c2272d" />
</shape>
</rotate>
</item>
<item android:top="0dp" android:bottom="0dp" android:left="0dp" android:right="0dp">
<rotate
android:fromDegrees="247.5"
android:toDegrees="247.5"
android:pivotX="50%"
android:pivotY="50%">
<shape android:shape="oval">
<size
android:width="800dp"
android:height="800dp"/>
<stroke
android:dashWidth="290.6dp"
android:dashGap="5000.01dp"
android:width="60dp"
android:color="#767171" />
</shape>
</rotate>
</item>
</layer-list>
这是我用来显示和更新的代码:
public class ShotMagFragment extends Fragment implements DataDelegate, View.OnClickListener{
private ImageView mainImage;
private ArrayList<Drawable> layers = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_shot_mag, container, false);
View simulateBtn = view.findViewById(R.id.simulateBtn);
simulateBtn.setOnClickListener(this);
mainImage = (ImageView)view.findViewById(R.id.mainImage);
return view;
}
public void displayNewImage() {
Random r = new Random();
int number = 1 + (20 - 20) * r.nextInt();
Drawable d = getResources().getDrawable(getResources().getIdentifier("mag_shade" + number, "drawable", getContext().getPackageName()));
layers.add(d);
final LayerDrawable layerDrawable = new LayerDrawable(layers.toArray(new Drawable[layers.size()]));
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mainImage.setImageDrawable(layerDrawable);
}
});
}
@Override
public void onClick(View v) {
displayNewImage();
}
}
但是当我使用不同的 XML 可绘制对象时,我不会遇到相同的 UI 锁定问题:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="130dp" android:bottom="130dp" android:left="130dp" android:right="130dp">
<rotate
android:fromDegrees="247.5"
android:toDegrees="247.5"
android:pivotX="50%"
android:pivotY="50%">
<shape android:shape="oval">
<size
android:width="540dp"
android:height="540dp"/>
<stroke
android:dashWidth="212.0dp"
android:dashGap="5000.01dp"
android:width="4dp"
android:color="#fff" />
</shape>
</rotate>
</item>
</layer-list>
编辑 在我对可绘制对象的引用旁边,我还会看到一个奇怪的错误图标。当我单击它时,它只会将我带到可绘制对象。我不知道这是否与我的问题有关,如果它是一个真正的错误,或者它是否只是一个通知。