图片动态操作,利用SeekBar控制属性示例,适配屏幕解决方案
需求为,让图片适配屏幕大小,并且可以用一个滑块来控制图片的旋转,用一个滑块来控制图片大小,核心语法思路,控制图片的大小,
核心语法为:mImageView.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight));
这句代码意思是,重新适配屏幕宽高,但是宽高是计算生成的,间接完成适配。适配方法如下:
但是这儿二个属性要提前配置,要求图片大不能超出屏幕,所以先计算屏幕大小,
1 2 3 4 5 | private void setMaxValueForSeekBar1(){ DisplayMetrics dmDisplayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dmDisplayMetrics); mSeekBar1.setMax(dmDisplayMetrics.widthPixels - MIN_WIDTH); } //这儿先配置滑块的最大数值图片旋转的配置核心代码为:Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.test1)).getBitmap();Matrix matrix = new Matrix();matrix.setRotate(i);bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(), bitmap.getHeight(),matrix,true);mImageView.setImageBitmap(bitmap); |
完整代码如下:
第一配置 values 文件 string.xml文件如下
1 2 3 4 5 6 7 | <resources> <string name= "app_name" >My Application1</string> <string name= "image_width" >图像宽度</string> <string name= "image_height" >图像高度</string> <string name= "image_rotate" >旋转度数</string> <string name= "init_rotate" >初始角度</string> </resources> |
第二静态布局文件如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | <?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http://schemas.android.com/apk/res-auto" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= ".MainActivity" > <ImageView android:id= "@+id/img" android:layout_marginTop= "20dp" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:scaleType= "fitCenter" android:src= "@drawable/test1" /> <TextView android:id= "@+id/content1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Hello World!" android:layout_marginTop= "10dp" /> <SeekBar android:id= "@+id/sekkbar1" android:layout_width= "match_parent" android:layout_height= "wrap_content" /> <TextView android:id= "@+id/content2" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "@string/init_rotate" /> <SeekBar android:id= "@+id/sekkbar2" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:max= "360" /> </LinearLayout> |
第三逻辑代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | package com.example.myapplication1; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.util.DisplayMetrics; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener { private final static int MIN_WIDTH = 200 ; private ImageView mImageView; private TextView mTextView1; private SeekBar mSeekBar1; private TextView mTextView2; private SeekBar mSeekBar2; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewByid(); setListners(); setMaxValueForSeekBar1(); } private void findViewByid(){ mImageView = findViewById(R.id.img); mTextView1 = findViewById(R.id.content1); mTextView2 = findViewById(R.id.content2); mSeekBar1 = findViewById(R.id.sekkbar1); mSeekBar2 = findViewById(R.id.sekkbar2); } private void setListners(){ mSeekBar1.setOnSeekBarChangeListener( this ); mSeekBar2.setOnSeekBarChangeListener( this ); } private void setMaxValueForSeekBar1(){ DisplayMetrics dmDisplayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dmDisplayMetrics); mSeekBar1.setMax(dmDisplayMetrics.widthPixels - MIN_WIDTH); } @Override // 当滑块被拖动时会执行以下代码 public void onProgressChanged(SeekBar seekBar, int i, boolean b) { if (seekBar.getId() == R.id.sekkbar1){ int newWidth = i + MIN_WIDTH; int newHeight = ( int )(newWidth* 3 / 4 ); mImageView.setLayoutParams( new LinearLayout.LayoutParams(newWidth,newHeight)); mTextView1.setText(getResources().getString(R.string.image_width) + newWidth+getResources().getString(R.string.image_height)+newHeight ); } else if (seekBar.getId() == R.id.sekkbar2){ Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.test1)).getBitmap(); Matrix matrix = new Matrix(); matrix.setRotate(i); bitmap = Bitmap.createBitmap(bitmap, 0 , 0 ,bitmap.getWidth(), bitmap.getHeight(),matrix, true ); mImageView.setImageBitmap(bitmap); mTextView2.setText(getResources().getString(R.string.image_rotate)+i); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } } |
第四屏幕截图,文件结构截图
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~