Android自己定義截屏功能,相似QQ截屏

來源:互聯網
上載者:User

標籤:starty   float   fse   簡單   技術   tor   public   ==   startx   

由於公司業務需求 須要對一個螢幕進行截屏。但內建的截屏功能是遠遠不夠項目的功能需求 ,我們是做一個畫板軟體 。須要的像QQ那樣截屏之後 ,能夠看到我們自己定義的工具。有畫筆,button等等 。android內建的功能非常easy,僅僅須要Intent隱式調用就全然足夠了。但他是系統的應用 ,介面固定。無法定製改動。實現方法跟辦法有非常多種,以下記錄下我實現的方法 。我是這樣一個思路 ,重寫一個View組件 ,在OnDraw裡面僅僅負責不繪圖形(包含半透明的四個矩形,亮框矩形,亮框上的四個小圓點),Ontouch方法是不停的去改動亮框 的座標點。然後又一次繪製 。

 
我是把這個圖片分解成以下這個圖的形狀的。

我們在onTouch裡面就不停地去繪製矩形跟圓點。


詳細代碼實現主要思路:

1、圖片繪製方法:

@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas); //不重寫 圖片無法出現if(mBitmap!=null){//drawNoLight(canvas);canvas.drawBitmap(mBitmap, iconLeft , iconTop, p_picture) ;//畫高亮的邊界drawRect(canvas) ;if(isDown)drawCircle(canvas) ;}}

2、圖片座標修改方法:
@Overridepublic boolean onTouchEvent(MotionEvent event) {int action = event.getAction() ;float x = event.getX() ;float y = event.getY() ;switch (action) {case MotionEvent.ACTION_DOWN:startX = x ;startY = y ;//須要推斷是在矩形的外邊還是裡面(推斷是移動還是縮放)if(x>lightRect.left+OFFSET && x<lightRect.right -OFFSET && y>lightRect.top+OFFSET && y<lightRect.bottom -OFFSET){//是移動的狀態isMove = true ;isScale = false ;}else if(x<lightRect.left-OFFSET || y<lightRect.top-OFFSET || x>lightRect.right+OFFSET || y>lightRect.bottom+OFFSET){isMove = false ;isScale = false ;}else {isMove = false ;isScale = true ; //縮放 point = getScalePoint(startX , startY);}if(!isScale)isDown = false ;break;case MotionEvent.ACTION_UP :case MotionEvent.ACTION_CANCEL:isDown = true ;break ;case MotionEvent.ACTION_MOVE:if(isMove){ //移動float dx = x - startX ;float dy = y - startY ;moveLightRect(dx , dy) ;startX = x ;startY = y ;isDown = false ;}if(isScale){float dx = x - startX ;float dy = y - startY ;resetLightRect(dx , dy) ;startX = x ;startY = y ;}break ;default:break;}invalidate() ;return true;}

3、圖片截取的方法:
public Bitmap getBitmap (){int x = (int)(lightRect.left - iconLeft) ;int y = (int)(lightRect.top - iconTop) ;int w = lightRect.right - lightRect.left ;int h = lightRect.bottom - lightRect.top ;Bitmap bitmap = Bitmap.createBitmap(mBitmap, x, y, w, h) ;return bitmap ;}

PS:這個僅僅是一個View   能夠實現圖片的截取,這時我們須要加一些自己定義的button進來。就使用一個布局檔案 。把button布置進去 。舉一個簡單的範例:
<?xml version="1.0" encoding="utf-8"?

><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <com.example.imagedemo.ImageTailor android:id="@+id/tailor" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_gravity="bottom" > <Button android:id="@+id/complete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="完畢" /> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="取消" /> </LinearLayout> </FrameLayout>


*:關鍵類和關鍵的方法我放在我的資源裡,須要的朋友能夠下載直接執行 看下效果。也能夠看也這個Demo。 主要是ImageTailor.java這個類的實現 。有什麼建議請大家提出來 ,共同學習。



Android自己定義截屏功能,相似QQ截屏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.