屏幕大小适配演示,综合练习。
功能为,用一个滑块来控制图片的旋转,用一个滑块来控制图片大小,核心语法思路,控制图片的大小,
mImageView.setLayoutParams(new LinearLayout.LayoutParams(newWidth,newHeight));
但是这儿二个属性要提前配置,并且图片大不能超出屏幕,所以先计算屏幕大小,
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文件如下
<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>
第二静态布局文件如下
<?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>
第三逻辑代码如下
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) { } }
第四屏幕截图,文件结构截图
标签:示例,SeekBar,安卓,private,bitmap,import,android,void From: https://www.cnblogs.com/fgxwan/p/17750467.html