日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
android矩形區(qū)域內(nèi)實(shí)現(xiàn)截圖
Android中實(shí)現(xiàn)矩形區(qū)域內(nèi)截圖,使用SurfaceView和Canvas進(jìn)行繪制。

在Android開發(fā)中,我們經(jīng)常需要實(shí)現(xiàn)截圖功能,而不僅僅是整個(gè)屏幕的截圖,我們只需要截取屏幕上的某一部分,例如一個(gè)矩形區(qū)域,如何在Android中實(shí)現(xiàn)矩形區(qū)域內(nèi)的截圖呢?本文將詳細(xì)介紹如何實(shí)現(xiàn)這一功能。

成都創(chuàng)新互聯(lián)公司長期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為大興企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì),大興網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

1. 獲取View的Bitmap

我們需要獲取到目標(biāo)矩形區(qū)域的View,然后將其轉(zhuǎn)換為Bitmap,這里我們可以使用以下方法:

public static Bitmap getViewBitmap(View view) {
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    return Bitmap.createBitmap(view.getDrawingCache());
}

2. 裁剪Bitmap

接下來,我們需要對獲取到的Bitmap進(jìn)行裁剪,只保留目標(biāo)矩形區(qū)域的內(nèi)容,這里我們可以使用以下方法:

public static Bitmap cropBitmap(Bitmap source, int x, int y, int width, int height) {
    return Bitmap.createBitmap(source, x, y, width, height);
}

3. 保存截圖

我們需要將裁剪后的Bitmap保存到本地或者分享給其他應(yīng)用,這里我們可以使用以下方法:

public static void saveBitmap(Bitmap bitmap, String filePath) {
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(filePath);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

4. 示例代碼

下面是一個(gè)完整的示例代碼,展示了如何在Android中實(shí)現(xiàn)矩形區(qū)域內(nèi)的截圖:

public class ScreenshotUtil {
    public static Bitmap getViewBitmap(View view) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        return Bitmap.createBitmap(view.getDrawingCache());
    }
    public static Bitmap cropBitmap(Bitmap source, int x, int y, int width, int height) {
        return Bitmap.createBitmap(source, x, y, width, height);
    }
    public static void saveBitmap(Bitmap bitmap, String filePath) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(filePath);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

使用方法:

// 獲取目標(biāo)矩形區(qū)域的View(例如一個(gè)ImageView)
ImageView imageView = findViewById(R.id.image_view);
// 獲取View的Bitmap
Bitmap sourceBitmap = ScreenshotUtil.getViewBitmap(imageView);
// 設(shè)置矩形區(qū)域的坐標(biāo)和大?。▁, y, width, height)
int x = 100; // 起始x坐標(biāo)
int y = 100; // 起始y坐標(biāo)
int width = 200; // 寬度
int height = 200; // 高度
// 裁剪Bitmap
Bitmap targetBitmap = ScreenshotUtil.cropBitmap(sourceBitmap, x, y, width, height);
// 保存截圖到本地(quot;/sdcard/screenshot.png")
ScreenshotUtil.saveBitmap(targetBitmap, "/sdcard/screenshot.png");

文章名稱:android矩形區(qū)域內(nèi)實(shí)現(xiàn)截圖
本文來源:http://www.5511xx.com/article/ccdehdd.html