gpt4 book ai didi

android - SeekBar 控制图片缩放

转载 作者:搜寻专家 更新时间:2023-11-01 09:43:17 25 4
gpt4 key购买 nike

这是seekbar的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) {

}
});
}
}

这是 Activity 的 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

即使在缩放时,我也希望图像位于固定区域内。但它正在从中走出来。任何解决方案。或者还有其他更好的方法可以使用搜索栏在特定区域缩放图像吗?

最佳答案

<?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>

关于android - SeekBar 控制图片缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38954734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com