這是搜索條的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>
這是輸出
我想要的形象即使在變焦時也要在固定區域內。但它正在走出來。任何解決方案或者還有其他更好的方法來使用seekbar縮放特定區域中的圖像嗎?
把ImageView的具有固定寬度/高度 –
的佈局內得到ImageView的固定的寬度和高度以及scaleType centerInside – lelloman
@MuratK。我不知道設備的分辨率。圖像應該與父寬度和高度匹配,它應該與其運行的所有設備相關。 – Krishna