溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android實(shí)現(xiàn)bitmap指定區(qū)域滑動(dòng)截取功能

發(fā)布時(shí)間:2020-10-02 16:01:37 來(lái)源:腳本之家 閱讀:178 作者:吃泥巴貓 欄目:移動(dòng)開(kāi)發(fā)

突然不知道什么心態(tài),說(shuō)要做這個(gè),網(wǎng)上找了半天沒(méi)找到合適的,就自己做了一個(gè)。

先上效果圖:

Android實(shí)現(xiàn)bitmap指定區(qū)域滑動(dòng)截取功能

透明區(qū)域?yàn)閷⒁厝〉膮^(qū)域,其他陰影部位為舍棄區(qū)域

圖片資源我寫死儲(chǔ)存在了raw中,有需要可以自己寫獲取bitmap。

界面layout:

<RelativeLayout 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"
  tools:context="com.admin.myapplication.MainActivity">
  <com.admin.myapplication.ScreenShotView
    android:id="@+id/screenShotView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
  <LinearLayout
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:background="#000000">
    <TextView
      android:layout_width="match_parent"
      android:layout_height="20dp"
      android:layout_gravity="center"
      android:gravity="center"
      android:text="選擇要截取區(qū)域"/>
  </LinearLayout>
  <LinearLayout
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:layout_alignParentBottom="true"
    android:background="#000000"
    android:orientation="horizontal">
    <TextView
      android:id="@+id/cancel_btn"
      android:text="取消"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:layout_gravity="center_vertical"
      android:gravity="center"/>
    <TextView
      android:id="@+id/certain_btn"
      android:text="確定"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:layout_gravity="center_vertical"
      android:gravity="center"/>
    <TextView
      android:id="@+id/restart_btn"
      android:text="重試"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:layout_gravity="center_vertical"
      android:gravity="center"/>
  </LinearLayout>

</RelativeLayout>

ScreenShotView為自定義View用來(lái)顯示bitmap,以及滑動(dòng)截圖,其中Dot類用來(lái)儲(chǔ)存坐標(biāo)點(diǎn)的x,y值。

public class ScreenShotView extends View {
  private Dot startDot;
  private Dot endDot;
  private Bitmap mBitmap;
  private Bitmap ocrBitmap;
  private int screenHeight;
  private int screenWidth;
  private Dot leftTopDot;
  private Dot rightBottomDot;
  private Paint paintShadow;
  int shadow = 0xaa000000;
  int clear = 0x0000000;


  public ScreenShotView(Context context) {
    super(context);
    startDot = new Dot();
    endDot = new Dot();
    leftTopDot = new Dot();
    rightBottomDot = new Dot();
  }

  public ScreenShotView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    startDot = new Dot();
    endDot = new Dot();
    leftTopDot = new Dot();
    rightBottomDot = new Dot();
  }

  public ScreenShotView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    startDot = new Dot();
    endDot = new Dot();
    leftTopDot = new Dot();
    rightBottomDot = new Dot();
  }

  public void setBitmap(Bitmap bitmap, int screenHeight, int screenWidth) {
    mBitmap = bitmap;
    this.screenHeight = screenHeight;
    this.screenWidth = screenWidth;
    changeBitmapSize();
    invalidate();
  }

  public void restart(){
    startDot = new Dot();
    endDot = new Dot();
    leftTopDot = new Dot();
    rightBottomDot = new Dot();
    invalidate();
  }
  /**
  *將將要顯示的bitmap進(jìn)行變形,使其鋪滿屏幕
  *
  */
  private void changeBitmapSize() {
    int width = mBitmap.getWidth();
    int height = mBitmap.getHeight();
    float scaleWidth = ((float) screenWidth) / width;
    float scaleHeight = ((float) screenHeight) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height, matrix, true);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    getLeftTopDot();
    getRightBottomDot();
    drawBitmap(canvas);
    drawArea(canvas);
    drawShadowTop(canvas);
    drawShadowLeft(canvas);
    drawShadowRight(canvas);
    drawShadowBottom(canvas);
  }

  /**
  *繪制陰影
  *
  */
  private void drawShadowBottom(Canvas canvas) {
    paintShadow = new Paint();
    paintShadow.setColor(shadow);
    canvas.drawRect(0, rightBottomDot.getY(), screenWidth, screenHeight, paintShadow);
  }

  private void drawShadowRight(Canvas canvas) {
    paintShadow = new Paint();
    paintShadow.setColor(shadow);
    canvas.drawRect(rightBottomDot.getX(), leftTopDot.getY(), screenWidth, rightBottomDot.getY(), paintShadow);
  }

  private void drawShadowLeft(Canvas canvas) {
    paintShadow = new Paint();
    paintShadow.setColor(shadow);
    canvas.drawRect(0, leftTopDot.getY(), leftTopDot.getX(), rightBottomDot.getY(), paintShadow);
  }

  private void drawShadowTop(Canvas canvas) {
    paintShadow = new Paint();
    paintShadow.setColor(shadow);
    canvas.drawRect(0, 0, screenWidth, leftTopDot.getY(), paintShadow);
  }

  private void drawBitmap(Canvas canvas) {
    Paint paint = new Paint();
    canvas.drawBitmap(mBitmap, 0, 0, paint);
  }

  /**
   * 畫(huà)出截圖區(qū)域
   *
   * @param canvas
   */
  private void drawArea(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(clear);
    canvas.drawRect(leftTopDot.getX(), leftTopDot.getY(), rightBottomDot.getX(), rightBottomDot.getY(), paint);
  }


  /**
   * 獲取截圖區(qū)域bitmap
   *
   * @return 截圖
   */
  public Bitmap getBitmap() {
    if (mBitmap != null) {
      getLeftTopDot();
      getRightBottomDot();
      if (getBitmapOutWidth() > 0 && getBitmapOutHeight() > 0) {
        if(leftTopDot.getY()<0){
          leftTopDot.setY(0);
        }
        ocrBitmap = Bitmap.createBitmap(mBitmap, (int) leftTopDot.getX(), (int) leftTopDot.getY(), getBitmapOutWidth(), getBitmapOutHeight());
      }
    }
    return ocrBitmap;
  }

  /**
   * 獲取截圖區(qū)域?qū)挾?   *
   * @return
   */
  private int getOutWidth() {
    return (int) (rightBottomDot.getX() - leftTopDot.getX());
  }

  /**
   * 獲取截圖區(qū)域高度
   *
   * @return
   */
  private int getOutHeight() {
    return (int) (rightBottomDot.getY() - leftTopDot.getY());
  }

  private int getBitmapOutWidth() {
    int bitmapOutWidth;
    int scale = getOutWidth() * mBitmap.getWidth();
    bitmapOutWidth = scale / screenWidth;
    return bitmapOutWidth;
  }

  private int getBitmapOutHeight() {
    int bitmapOutHeight;
    int scale = getOutHeight() * mBitmap.getHeight();
    bitmapOutHeight = scale / screenHeight;
    return bitmapOutHeight;
  }

  private void getLeftTopDot() {
    if (endDot.getX() > startDot.getX()) {
      leftTopDot.setX(startDot.getX());
    } else {
      leftTopDot.setX(endDot.getX());
    }
    if (endDot.getY() > startDot.getY()) {
      leftTopDot.setY(startDot.getY());
    } else {
      leftTopDot.setY(endDot.getY());
    }
  }

  private void getRightBottomDot() {
    if (startDot.getX() > endDot.getX()) {
      rightBottomDot.setX(startDot.getX());
    } else {
      rightBottomDot.setX(endDot.getX());
    }
    if (startDot.getY() > endDot.getY()) {
      rightBottomDot.setY(startDot.getY());
    } else {
      rightBottomDot.setY(endDot.getY());
    }

  }

  public Dot getStartDot() {
    return startDot;
  }

  public void setStartDot(Dot startDot) {
    this.startDot = startDot;
  }

  public Dot getEndDot() {
    return endDot;
  }

  public void setEndDot(Dot endDot) {
    this.endDot = endDot;
  }

}

MainActivity中完成對(duì)點(diǎn)擊滑動(dòng)的監(jiān)控,通過(guò)坐標(biāo)點(diǎn)的方式獲得需要繪制的矩形位置和形狀。

public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnClickListener {
  private ScreenShotView screenShotView;
  private Bitmap bmp;
  private Bitmap ocrBitmap;
  private TextView certainBtn;
  private TextView cancelBtn;
  private TextView restartBtn;
  private int screenWidth;
  private int screenHeight;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    screenShotView = (ScreenShotView) findViewById(R.id.screenShotView);
    cancelBtn = (TextView) findViewById(R.id.cancel_btn);
    cancelBtn.setOnClickListener(this);
    certainBtn = (TextView) findViewById(R.id.certain_btn);
    certainBtn.setOnClickListener(this);
    restartBtn = (TextView)findViewById(R.id.restart_btn);
    restartBtn.setOnClickListener(this);
    DisplayMetrics dm = new DisplayMetrics();
    //獲取屏幕信息
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    screenWidth = dm.widthPixels;
    screenHeight = dm.heightPixels;
    Resources r = this.getResources();
    InputStream is = r.openRawResource(R.raw.bg);
    BitmapDrawable bmpDraw = new BitmapDrawable(is);
    bmp = bmpDraw.getBitmap();
    screenShotView.setBitmap(bmp, screenHeight, screenWidth);
    screenShotView.setOnTouchListener(this);
  }

  @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {
    switch (motionEvent.getAction()) {
      case MotionEvent.ACTION_DOWN:
        screenShotView.setStartDot(new Dot(motionEvent.getX(), motionEvent.getY()));
        break;
      case MotionEvent.ACTION_MOVE:
        screenShotView.setEndDot(new Dot(motionEvent.getX(), motionEvent.getY()));
        screenShotView.setBitmap(bmp, screenHeight, screenWidth);
        break;
      case MotionEvent.ACTION_UP:
        ocrBitmap = screenShotView.getBitmap();
        break;
    }
    return true;
  }

  @Override
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.cancel_btn:
        finish();
        break;
      case R.id.certain_btn:
        if (ocrBitmap != null) {
          BitmapUtil.getInstance().setImageBitmap(ocrBitmap);
          Intent intent = new Intent(MainActivity.this, Main2Activity.class);
          startActivity(intent);
        }else{
          Toast.makeText(MainActivity.this,"請(qǐng)選擇截取區(qū)域",Toast.LENGTH_SHORT).show();
        }
        break;
      case R.id.restart_btn:
        screenShotView.restart();
        break;
    }
  }

  @Override
  protected void onResume() {
    super.onResume();
    screenShotView.restart();
  }

  @Override
  protected void onRestart() {
    super.onRestart();
    screenShotView.restart();
  }
}

運(yùn)用了單例模式用來(lái)存儲(chǔ)截取出來(lái)的bitmap,方便跳轉(zhuǎn)時(shí)調(diào)用,不需要自己再寫類,點(diǎn)擊確定后,會(huì)將區(qū)域中的bitmap提取并存儲(chǔ)在單例中,在下一個(gè)頁(yè)面再調(diào)用。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI