触摸手势后,TouchImageView才会显示图像 [英] TouchImageView doesn't show image until after a pinch gesture

查看:163
本文介绍了触摸手势后,TouchImageView才会显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义视图 TouchImageView .但是,当我设置位图时,除非在其上进行捏手势,否则它不会显示图像.

I'm using the custom view TouchImageView. However, when I set the bitmap it doesn't show the image unless I do a pinch gesture on it.

public class PhotoOverlayActivity extends AppCompatActivity {

    public static final String CURRENT_MESSAGE_KEY = "message";

    private static final int IMAGE_REQUEST_CODE = 0;

    String currentMessage;
    private Bitmap bitmap;
    private TouchImageView mImageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_overlay);

        setupToolbar();
        setupImageView();

        currentMessage = getIntent().getStringExtra(CURRENT_MESSAGE_KEY);
        chooseImage();
    }

    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowHomeEnabled(true);
            actionBar.setTitle("");
        }
    }

    private void setupImageView() {
        mImageView = findViewById(R.id.imageView);

    }

    private void chooseImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, IMAGE_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == IMAGE_REQUEST_CODE && resultCode == Activity.RESULT_OK)
            try {
                if (bitmap != null) {
                    bitmap.recycle();
                }
                InputStream stream = getContentResolver().openInputStream(data.getData());
                bitmap = BitmapFactory.decodeStream(stream);
                stream.close();
                int nh = (int) (bitmap.getHeight() * (512.0 / bitmap.getWidth()));
                Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 512, nh, true);
                mImageView.setImageBitmap(scaled);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        super.onActivityResult(requestCode, resultCode, data);
    }

}

我终于在此处找到了解决方案,所以我在下面发布了我的答案

I finally found the solution buried here, so I am posting my answer below.

推荐答案

调用

mImageView.setImageBitmap(yourBitmap);

mImageView.setZoom(1f);

Another option would be to modify the source code to do the same thing since the TouchImageView class is contained in your project anyway.

感谢这篇文章.

这篇关于触摸手势后,TouchImageView才会显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆