这是搜索栏的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>
这是输出
我希望即使在变焦时图像也在固定区域内。但它正在走出它。任何解决方案或者还有其他更好的方法可以使用搜索条缩放特定区域的图像吗?
答案 0 :(得分: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>
答案 1 :(得分:1)
我遇到了类似的问题(从10变为100),它对我有用
float scale = ((progress / 100.0f) + 1);
iv.setScaleX(scale);
iv.setScaleY(scale);