setScaleType to ScaleType.MATRIX programmatically

2020-07-06 04:24发布

问题:

this is my code :

    LinearLayout imageViewParent = (LinearLayout) findViewById(R.id.imageViewParent);
    ImageView view = new ImageView(this);
    imageViewParent.addView(view);
    view.setScaleType(ScaleType.MATRIX);
    view.setBackgroundResource(R.drawable.np);

In this code scale type to matrix not work(it fix image to parents),but when I implement this in xml it works fine! can every one help me? thank you!

回答1:

You should use setImageResource not setBackgroundResource.



回答2:

Use this:

view.setImageResource(R.drawable.np);