ImageView ScaleType属性

2017-10-25  本文已影响0人  MLLWF

ScaleType属性

ScaleType属性的值可以分三种类型:

详解

imageView.setScaleType(ImageView.ScaleType.MATRIX);
再调用
imageView.setImageMatrix(matrix);

imageView.setScaleType(ImageView.ScaleType.MATRIX);  //设置为矩阵模式

Matrix matrix = new Matrix();           //创建一个单位矩阵
matrix.setTranslate(100, 100);          //平移x和y各100单位
matrix.preRotate(30);                   //顺时针旋转30度
imageView.setImageMatrix(matrix);       //设置并应用矩阵
MATRIX
简单的图片放大示例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <ImageView
        android:id="@+id/image2"
        android:layout_width="220dp"
        android:layout_height="220dp" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:src="@mipmap/mllwf" />

</LinearLayout>
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_view);
        BaseInit.getInstance().init(getApplication(), true);
        final ImageView imageView1 = (ImageView) findViewById(R.id.image1);
        final ImageView imageView2 = (ImageView) findViewById(R.id.image2);

        imageView1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                BitmapDrawable drawable = (BitmapDrawable) imageView1.getDrawable();
                Bitmap bitmap = drawable.getBitmap();
                int height = imageView2.getHeight();
                int dipHeight = ConvertUtils.px2dip(MainActivity.this, height);
                L.e("dipHeight:" + dipHeight);
                double sacle = 1.0 * bitmap.getHeight() / imageView1.getHeight();
                int x = (int) (event.getX() * sacle);
                int y = (int) (event.getY() * sacle);
                int center = dipHeight / 2;
                L.e("center:" + center);
                if (x + center >= bitmap.getWidth()) {
                    x = bitmap.getWidth() - center;
                }
                if (y + center >= bitmap.getHeight()) {
                    y = bitmap.getHeight() - center;
                }
                if (x - center <= 0) {
                    x = center;
                }
                if (y - center <= 0) {
                    y = center;
                }
                Bitmap bitmap1 = Bitmap.createBitmap(bitmap, x - center, y - center, dipHeight, dipHeight);
                imageView2.setImageBitmap(bitmap1);
                return true;
            }
        });
    }
效果图.png

CircleImageView(圆形ImageView)

源码地址

上一篇下一篇

猜你喜欢

热点阅读