2016-08-15 65 views
1

这是搜索条的onProgressChangeListener搜索栏来控制图像缩放

package com.example.android.seekbartest; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.ImageView; 
import android.widget.SeekBar; 

public class MainActivity extends AppCompatActivity { 

SeekBar sb; 
ImageView iv; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    iv = (ImageView) findViewById(R.id.imageView); 
    sb = (SeekBar) findViewById(R.id.seekBar); 
    sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { 
      float scale = ((progress/10.0f)+1); 
      iv.setScaleX(scale); 
      iv.setScaleY(scale); 
     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 

     } 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 

     } 
    }); 
} 
} 

这是活动的XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.android.seekbartest.MainActivity"> 

<ImageView 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:id="@+id/imageView" 
    android:src="@drawable/test" 
    android:adjustViewBounds="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

<SeekBar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:max="15" 
    android:layout_below="@id/imageView" 
    android:layout_marginTop="20dp" 
    android:id="@+id/seekBar" 
    android:maxHeight="40dp" 
    android:minHeight="40dp" 
    android:thumbOffset="18dp"/> 
</RelativeLayout> 

这是输出

enter image description here

我想要的形象即使在变焦时也要在固定区域内。但它正在走出来。任何解决方案或者还有其他更好的方法来使用seekbar缩放特定区域中的图像吗?

+0

把ImageView的具有固定宽度/高度 –

+0

的布局内得到ImageView的固定的宽度和高度以及scaleType centerInside – lelloman

+0

@MuratK。我不知道设备的分辨率。图像应该与父宽度和高度匹配,它应该与其运行的所有设备相关。 – Krishna

回答

1
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.android.seekbartest.MainActivity"> 

<LinearLayout 
    android:layout_height="100dp" 
    android:layout_width="match_parent"> 
<!-- Add the height and width over here, so that your image view doesnot exceed the LinearLayout frame --> 
<ImageView 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:id="@+id/imageView" 
    android:src="@drawable/test" 
    android:adjustViewBounds="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /></LinearLayout> 

<SeekBar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:max="15" 
    android:layout_below="@id/imageView" 
    android:layout_marginTop="20dp" 
    android:id="@+id/seekBar" 
    android:maxHeight="40dp" 
    android:minHeight="40dp" 
    android:thumbOffset="18dp"/> 
</RelativeLayout> 
+0

我不会知道设备分辨率的权利。图像应该与父宽度和高度匹配,它应该与其运行的所有设备相关。 – Krishna

+0

您可以通过LinearLayout的程序设置宽度!或者您可以选择选择布局的某个“高度”。并为'width'保留'match-parent' – Smit