控件里面的进度条条Sekbar的用法,下面将用Imageview来具体的演示。

首先我的xml代码

<?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">
    <ImageView
        android:id="@+id/IM"
        android:layout_width="500px"
        android:layout_height="500px"
        android:scaleType="fitCenter"
        android:src="@mipmap/str"
        />
    <TextView
        android:id="@+id/TV1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="图片大小:"
        android:textColor="#FFFF3333"
        />
    <SeekBar
        android:id="@+id/SK1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="240"
        android:progress="150"
        />
    <TextView
        android:id="@+id/TV2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="图片旋转度数:"
        android:textColor="#FFFF3333"
        />
    <SeekBar
        android:id="@+id/SK2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="360"
        android:progress="360" />


</LinearLayout>

<SeekBar
android:minHeighthuo或(maxHeight)来控制progress的粗细(就是滑动条的粗细)
android:layout_width=“match_parent” 整个进度条的宽度
android::layout_height=“wrap_content” 整个进度条的高度
android:progressDrawable 设置进度条的样式(包括背景样式,和颜色)
android:Max 滑动条最大的滑动范围
android:progress 初始滑动条的位置
android:thumb 滑动条的图标
/> 等重要属性
它的各种方法:
getMax() 获取最大的范围值
getprogress() 获取当前进度值
setMax() 设置范围最大值
getMax() 获取最大范围值
getProgress() 获取当前进度值
setMax(int) 设置范围最大值

package com.example.myimageview;

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 {
    public TextView TV1,TV2;
    public SeekBar SK1,SK2;
    public ImageView IM;
    public int minWidth=350;
    public Matrix matrix=new Matrix();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TV1=(TextView)findViewById(R.id.TV1);
        TV2=(TextView)findViewById(R.id.TV2);
        SK1=(SeekBar) findViewById(R.id.SK1);
        SK2=(SeekBar) findViewById(R.id.SK2);
        IM=(ImageView)findViewById(R.id.IM) ;
        DisplayMetrics dm=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        SK1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                    int newWidth=progress+minWidth;
                    int newHeight=newWidth;
                    IM.setLayoutParams(new LinearLayout.LayoutParams(newWidth, newHeight));
                    TV1.setText("图像宽度:"+newWidth+"  "+"图像高度:"+newHeight);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        SK2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                BitmapDrawable bitmapDrawable=(BitmapDrawable) getResources().getDrawable(R.mipmap.str);//通过路径找到mipmap中的str
                //Resources res=getResources();得到路径
                //getDrawable(R.mipmap.str)得到图像
                // 实际上这是一个BitmapDrawable对象
                Bitmap bitmap=bitmapDrawable.getBitmap();//可以在调用getBitmap方法,得str到这个位图
                matrix.setRotate(progress);//Matrix.rotate(弧度); //弧度就是 (角度/ 180)* Math.PI
                bitmap=bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,true);
                IM.setImageBitmap(bitmap);
                TV2.setText("图片旋转"+progress+"度");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

结果如图
在这里插入图片描述

		IM.setPivotX(IM.getWidth()/2);
     	 IM.setPivotY(IM.getHeight()/2);//支点在图片中心
       	IM.setRotation(progress)

这种旋转方法效果图如下
在这里插入图片描述

  1. Resources res=getResources();
  2. Drawable drawable = res.getDrawable(R.drawable.myimage);
  3. //实际上这是一个BitmapDrawable对象
  4. BitmapDrawable bitmapDrawable=(BitmapDrawable)drawable;
  5. //可以在调用getBitmap方法,得到这个位图
  6. Bitmap bitmap=bitmapDrawable.getBitmap();
    针对这一段代码详细的解析一下

BitmapDrawable bitmapDrawable=(BitmapDrawable) getResources().getDrawable(R.mipmap.str);//通过路径找到mipmap中的str
//Resources res=getResources();得到路径
//getDrawable(R.mipmap.str)得到图像
// 实际上这是一个BitmapDrawable对象
Bitmap bitmap=bitmapDrawable.getBitmap();
//可以在调用getBitmap方法,得str到这个位图

这一段代码我找出了相关的方法和参数

bitmap=bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,true);
(一) Bitmap方法相关参数:
(1) Bitmap createBitmap (Bitmap src)
从原位图src复制出一个新的位图,和原始位图相同

(2) Bitmap createBitmap (int width, int height, Bitmap.Config config)
根据参数创建新位图
int width The width of the bitmap
int height The height of the bitmap
config The bitmap config to create

(3) Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
返回一个不可变的源位图的位图的子集,改变了可选的矩阵。新的位图可能与源相同的对象,或可能是一个副本。
它初始化与原始位图的密度。如果源位图是不可变的,请求的子集是一样的源位图本身,然后返回源位图,没有新的位图创建。
(从原始位图剪切图像,这是一种高级的方式。可以用Matrix(矩阵)来实现旋转等高级方式截图)

sourceBitmap 产生子位图的源位图;
x int 子位图第一个像素在源位图的X坐标
y int 子位图第一个像素在源位图的y坐标
width int 子位图每一行的像素个数
height int 子位图的行数
m Matrix 对像素值进行变换的可选矩阵
filter boolean 如果为true,源图要被过滤。该参数仅在matrix包含了超过一个翻转才有效
Returns Bitmap 一个描述了源图指定子集的位图

(4) public static Bitmap createBitmap (int[] colors, int width, int height, Bitmap.Config config)
这个函数根据颜色数组来创建位图,注意:颜色数组的长度>=width*height
(此函数创建位图的过程可以简单概括为为:更加width和height创建空位图,然后用指定的颜色数组colors来从左到右从上至下一次填充颜色。
config是一个枚举,可以用它来指定位图“质量”。)

(5) public static Bitmap createBitmap (int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)
offset 写入到pixels[]中的第一个像素索引值
stride pixels[]中的行间距个数值(必须大于等于位图宽度)
width和height指位图的宽高

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值