未知的位图配置,应用程序在启动时崩溃

时间:2017-09-07 17:40:52

标签: java android bitmap

resId不会返回0但应用程序崩溃并记录

  

java.lang.IllegalArgumentException:未知位图配置

当我创建位图背景时。

public class MainFragment extends Fragment {
    static int mheight, mwidth = 0;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.main_fragment, container, false);
    ListView updates = view.findViewById(R.id.list_view);
    //older updates to be shown here
    ArrayList<String> fetcheddata = new ArrayList<>();
    fetcheddata.add(0, "sample data");
    fetcheddata.add(1, "sample data");
    fetcheddata.add(2, "sample data");

    //main image is id for imageview in xml
    ImageView main_image = view.findViewById(R.id.mainimage);
    //getting height and width of imageview , having no android:src 0dp
    mheight = main_image.getMaxHeight();
    mwidth = main_image.getMaxWidth();
    //getting big values in log
    Log.v("NNNNNNNNN", "" + mwidth + "," + mheight);
    // main_image.setImageResource(R.drawable.do_dont);


    String imageName = "picture";
    //getting resource id
    int resID = getActivity().getResources().getIdentifier("@drawable/do_dont", "drawable", getActivity().getPackageName());
    Log.v("resource id ", "" + resID);
    ImageView image = view.findViewById(R.id.mainimage);
    image.setVisibility(View.INVISIBLE);
    image.setImageResource(resID);
    image.buildDrawingCache();
    Bitmap bmap = image.getDrawingCache();

    Bitmap background = Bitmap.createBitmap((int) mheight, (int) mwidth, Bitmap.Config.RGB_565);
    //getting image properites (image contained in drawable )
    float originalWidth = image.getWidth();
    float originalHeight = image.getHeight();

    Canvas canvas = new Canvas(background);

    float scale = mwidth / originalWidth;

    float xTranslation = 0.0f;
    float yTranslation = (mheight - originalHeight * scale) / 2.0f;

    Matrix transformation = new Matrix();
    transformation.postTranslate(xTranslation, yTranslation);
    transformation.preScale(scale, scale);

    Paint paint = new Paint();
    paint.setFilterBitmap(true);
    canvas.drawBitmap(bmap, transformation, paint);
    main_image.setImageBitmap(background);
    main_image.setVisibility(View.VISIBLE);


    ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.mainfragment_listview, fetcheddata);
    updates.setAdapter(adapter);
    return view;
}
}

0 个答案:

没有答案