如何在运行时启用/禁用缩放?

时间:2015-07-06 10:02:47

标签: android zoom android-imageview image-zoom

我正在处理图像,我将全屏显示图像,当用户触摸屏幕时,将显示一些控件。因此,我想在屏幕上显示控件时禁用缩放。

或者有人告诉我如何检测图像是否缩放?

activity_image.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
    <ImageView
        android:id="@+id/imgSelected"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:scaleType="centerInside"            
        android:src="@drawable/placeholder"
        android:contentDescription="@string/image" />
    <LinearLayout
        android:id="@+id/imageControls"
        android:layout_width="match_parent"
        android:layout_height="match_parent"            
        android:gravity="bottom"  
        android:weightSum="9"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"            
        android:orientation="horizontal"   
        android:visibility="gone"                  
        android:background="@drawable/image_controls">
        <Button 
            android:id="@+id/btnSetAsBackground"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="left|center_vertical"
            android:background="#00000000"
            style="@style/ImageControls"
            android:text="Set"/>
        <Button 
            android:id="@+id/btnFavorite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:background="#00000000"
            style="@style/ImageControls"
            android:text="Favourite"/>
        <Button 
            android:id="@+id/btnShare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="right|center_vertical"
            android:background="#00000000"
            style="@style/ImageControls"
            android:text="Share"/>
    </LinearLayout>        
</RelativeLayout>

activity_zoom.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center" >
</LinearLayout>

ImageActivity.java

public class ImageActivity extends Activity {
private int imageId;
private int categoryId;
private String imageUrl;
private ZoomView zoomView;
private ImageView imageView;
private LinearLayout main_container;
private boolean controlFlag;
private LinearLayout imageControls;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zoom);             
    if (Build.VERSION.SDK_INT < 16) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);            
    }
    final View convertView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_image, null, false);
    convertView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    imageControls = (LinearLayout) convertView.findViewById(R.id.imageControls);
    controlFlag = false;
    zoomView = new ZoomView(this);
    imageId = getIntent().getExtras().getInt("image");
    categoryId = getIntent().getExtras().getInt("category");
    imageView = (ImageView) convertView.findViewById(R.id.imgSelected);
    imageUrl = SplashActivity.urlList.get(categoryId).get(imageId);
    Log.e(String.valueOf(categoryId),imageUrl);
    Picasso
    .with(this)
    .load(imageUrl)             
    .placeholder(R.drawable.placeholder)
    .into(imageView, new Callback() {
        @Override
        public void onError() {         

        }
        @Override
        public void onSuccess() {
            imageView.setScaleType(ScaleType.FIT_CENTER);               
        }
    }); 
    zoomView.addView(convertView);
    main_container = (LinearLayout) findViewById(R.id.main_container);
    main_container.addView(zoomView);
    convertView.setOnTouchListener(new OnTouchListener() {          
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {               
            if(controlFlag) {
                imageControls.setVisibility(View.GONE);
                controlFlag = false;
            } else {        
                imageControls.setVisibility(View.VISIBLE);
                controlFlag = true;
            }
            return false;
        }
    });
}   

}

我使用android-zoom-view.jar库进行缩放。我想仅在图像不缩放时才显示LinearLayout (imageControls)如果LinearLayout (imageControls)可见则禁用缩放。由于ZoomView实例会对所有活动执行缩放,因此imageControls也将进行缩放。这不是一个良好的用户友好设计。

所以请帮我改进这个布局。

...谢谢

1 个答案:

答案 0 :(得分:2)

在zoomview课程中考虑使用此https://github.com/Polidea/android-zoom-view/blob/master/src/pl/polidea/view/ZoomView.java  只需添加此

convertView.setOnTouchListener(new OnTouchListener() {          
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {               
        if(controlFlag) {
            imageControls.setVisibility(View.GONE);
            controlFlag = false;
        } else {        
            imageControls.setVisibility(View.VISIBLE);
            controlFlag = true;
        }
        zoomView.setIsZoomEnable(!controlFlag);
        return false;
    }
});

然后在你的代码中

WITH summary AS 
(
   SELECT  Msisdn, DateRegistered , 
       RANK() OVER  (ORDER BY DateRegistered ASC) AS ASCRank, 
       RANK() OVER  (ORDER BY DateRegistered DESC) AS DESCRank 
   FROM dbo.SubscriptionsArchive 
   WHERE MSISDN='123456'
)
SELECT s.msisdn,
(CASE WHEN ASCRAnk=1 THEN s.DateRegistered END)  AS firstReg,
(CASE WHEN DESCRAnk=1 THEN s.DateRegistered END)  AS LASTReg
FROM summary s