版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Android中“用放大鏡效果查看圖庫(kù)中的圖”的代碼清單范美英(北京信息職業(yè)技術(shù)學(xué)院)摘 要:Android中可以自定義視圖,只需要將視圖類繼承自View,并重寫其onDraw方法即可。本文詳細(xì)羅列了Android應(yīng)用界面中自定義視圖,并“用放大鏡效果查看圖庫(kù)中的圖”的代碼清單。關(guān)鍵詞: Android;自定義視圖;圖庫(kù) 1 src/MainActivity.java類的代碼package com.book.testmagnifyphoto;import android.app.Activity;import android.content.Intent;import android.data
2、base.Cursor;import .Uri;import android.os.Bundle;import vider.MediaStore;import android.view.View;public class MainActivity extends Activity /定義請(qǐng)求碼private static final int ChooseImage = 1;/聲明MagnifyView對(duì)象MagnifyView imgChosen;Overrideprotected void onCreate(Bundle savedInstanceState) supe
3、r.onCreate(savedInstanceState);this.setContentView(R.layout.activity_main);imgChosen = (MagnifyView) findViewById(R.id.img_chosen);public void clickChoose(View v)/啟動(dòng)圖庫(kù)activity,并帶回選擇的圖片Intent i = new Intent(Intent.ACTION_PICK, vider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);startActivi
4、tyForResult(i, ChooseImage);Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) super.onActivityResult(requestCode, resultCode, data);/如果正確選擇了圖片if (requestCode = ChooseImage && resultCode = RESULT_OK && data != null) /獲取選擇的圖片 Uri selectedImage = data
5、.getData(); String filePathColumn = MediaStore.Images.Media.DATA ; Cursor cursor = getContentResolver().query( selectedImage, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn0); String picturePath = cursor.getString(columnIndex); cursor.
6、close(); if(picturePath!=null) imgChosen.setBitmap(picturePath); 2 src/ MagnifyView.java類的代碼package com.book.testmagnifyphoto;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.BitmapShader;import android.graphics.Canvas;import
7、 android.graphics.Matrix;import android.graphics.Shader.TileMode;import android.graphics.drawable.ShapeDrawable;import android.graphics.drawable.shapes.OvalShape;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;public class MagnifyView
8、 extends View / 從圖庫(kù)中獲取到的圖像private Bitmap bitmap; / 放大后的圓形遮罩private ShapeDrawable drawable;/ 放大鏡的半徑,隨著分辨率的不同會(huì)發(fā)生改變private final int RADIUS = 38; / 放大倍數(shù)private final int FACTOR = 2; private Matrix matrix = new Matrix();/ 放大鏡位圖private Bitmap bitmap_magnifier; / 放大鏡的左邊距private int m_left = 0; / 放大鏡的頂邊距pr
9、ivate int m_top = 0;/ 構(gòu)造函數(shù)public MagnifyView(Context context, AttributeSet as) super(context,as);/獲取放大鏡圖像bitmap_magnifier = BitmapFactory.decodeResource(getResources(),R.drawable.magnifier);Overrideprotected void onDraw(Canvas canvas) super.onDraw(canvas);try/ 繪制居中的背景圖像canvas.drawBitmap(bitmap, (get
10、Width()-bitmap.getWidth()/2, (getHeight()-bitmap.getHeight()/2, null); / 繪制放大后的圖像drawable.draw(canvas); catch(Exception e)e.getCause();/ 繪制放大鏡canvas.drawBitmap(bitmap_magnifier, m_left, m_top, null);Overridepublic boolean onTouchEvent(MotionEvent event) / 獲取當(dāng)前觸摸點(diǎn)的橫坐標(biāo)final int x = (int) event.getX();
11、 / 獲取當(dāng)前觸摸點(diǎn)的縱坐標(biāo)final int y = (int) event.getY(); try/ 平移到繪制shader的起始位置/ 水平方向平移RADIUS - x * FACTOR,垂直方向平移RADIUS - y * FACTORmatrix.setTranslate(RADIUS - x * FACTOR, RADIUS - y * FACTOR );drawable.getPaint().getShader().setLocalMatrix(matrix);/ 設(shè)置圓的外切矩形,以此觸發(fā)drawable的繪制drawable.setBounds(x - RADIUS, y -
12、 RADIUS, x + RADIUS, y + RADIUS);catch(Exception e)e.getCause();/ 計(jì)算放大鏡的左邊距m_left = x - bitmap_magnifier.getWidth() / 2; / 計(jì)算放大鏡的右邊距m_top = y - bitmap_magnifier.getHeight() / 2; invalidate(); / 重繪畫布return true;/獲取要顯示的源圖,并放大圖public void setBitmap(String picturePath) /取得源圖bitmap= BitmapFactory.decodeF
13、ile(picturePath);/根據(jù)畫布寬、高縮放源圖bitmap= bitmap.createScaledBitmap(bitmap, getWidth(), getHeight(), true);/以FACTOR為倍數(shù)放大圖magnifyBitmap(bitmap,FACTOR);/生成放大效果圖private void magnifyBitmap(Bitmap bitmap, int factor) / TODO Auto-generated method stub/放大后的圖BitmapShader shader = new BitmapShader(Bitmap.createSc
14、aledBitmap(bitmap, bitmap.getWidth() * factor,bitmap.getHeight() * factor, true), TileMode.CLAMP,TileMode.CLAMP);/ 圓形遮罩drawable = new ShapeDrawable(new OvalShape();/ 將放大后的圖切除多余部分后放到drawable中drawable.getPaint().setShader(shader);/ 設(shè)置圓的外切矩形,以此觸發(fā)drawable的繪制drawable.setBounds(0, 0, RADIUS * 2, RADIUS *
15、2); / 此處計(jì)算放大鏡的邊距可以使得遮罩和放大鏡初始位置相同m_left = RADIUS - bitmap_magnifier.getWidth() / 2; m_top = RADIUS - bitmap_magnifier.getHeight() / 2; 3 res/layout/activity_main.xml的代碼清單<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" android:layout_width="ma
16、tch_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.book.testmagnifyphoto.MagnifyView android:id="+id/img_chosen" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9"/> <Button android:id="+id/btn_choose" android:la
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 信用評(píng)分模型優(yōu)化-深度研究
- 多模態(tài)文物信息融合-深度研究
- 團(tuán)簇材料生物成像-深度研究
- 公私合作模式-深度研究
- 家庭安全服務(wù)創(chuàng)新-深度研究
- 意識(shí)哲學(xué)前沿-深度研究
- 2025年廣東行政職業(yè)學(xué)院高職單招數(shù)學(xué)歷年(2016-2024)頻考點(diǎn)試題含答案解析
- 基于靜力變形指標(biāo)的鋼-混凝土組合梁損傷識(shí)別方法研究
- 2025年廣東機(jī)電職業(yè)技術(shù)學(xué)院高職單招語(yǔ)文2018-2024歷年參考題庫(kù)頻考點(diǎn)含答案解析
- 2025年廣東工貿(mào)職業(yè)技術(shù)學(xué)院高職單招高職單招英語(yǔ)2016-2024歷年頻考點(diǎn)試題含答案解析
- 《醫(yī)院財(cái)務(wù)分析報(bào)告》課件
- 2025老年公寓合同管理制度
- 2024-2025學(xué)年人教版數(shù)學(xué)六年級(jí)上冊(cè) 期末綜合卷(含答案)
- 2024中國(guó)汽車后市場(chǎng)年度發(fā)展報(bào)告
- 感染性腹瀉的護(hù)理查房
- 天津市部分區(qū)2023-2024學(xué)年高二上學(xué)期期末考試 物理 含解析
- 《人工智能基礎(chǔ)》全套英語(yǔ)教學(xué)課件(共7章)
- 廢鐵收購(gòu)廠管理制度
- 物品賠償單范本
- 《水和廢水監(jiān)測(cè)》課件
- 滬教版六年級(jí)數(shù)學(xué)下冊(cè)課件【全冊(cè)】
評(píng)論
0/150
提交評(píng)論