簡單實現(xiàn)Android放大鏡效果
利用之前學(xué)過的圖形圖像繪畫技術(shù)和圖片添加特效技術(shù),我們來實現(xiàn)一個Android放大鏡的簡單應(yīng)用。
最終效果如圖
具體實現(xiàn):
用來顯示自定義的繪圖類的布局文件
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/frameLayout1" android:orientation="vertical" > </FrameLayout>
打開MainActivity,在文件中創(chuàng)建名為MyView的內(nèi)部類,繼承android.view.View類,并添加構(gòu)造方法和重寫onDraw(Canvas canvas)方法,在里面進行作圖:
MainActivity:
package com.example.test; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Shader.TileMode; import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.OvalShape; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.FrameLayout; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //獲取布局文件中添加的幀布局管理器 FrameLayout fl=(FrameLayout)findViewById(R.id.frameLayout1); //將自定義的MyView視圖添加到幀布局 fl.addView(new MyView(this)); } public class MyView extends View{ private Bitmap bitmap;//源圖像,也就是背景圖像 private ShapeDrawable drawable; private final int RADIUS=57;//放大鏡的半徑 private final int FACTOR=2;//放大倍數(shù) private Matrix matrix=new Matrix(); private Bitmap bitmap_magnifiter;//放大鏡位圖 private int m_left=0;//放大鏡的左邊距 private int m_top=0;//放大鏡的頂邊距 public MyView(Context context) { super(context); //獲取要顯示的源圖像 Bitmap bitmap_source=BitmapFactory.decodeResource(getResources(), R.drawable.backgroud); bitmap=bitmap_source; BitmapShader shader=new BitmapShader(Bitmap.createScaledBitmap( bitmap_source, bitmap_source.getWidth()*FACTOR, bitmap_source.getHeight()*FACTOR, true),TileMode.CLAMP, TileMode.CLAMP);//創(chuàng)建BitmapShader對象 /* 注:Bitmap.createScaledBitmap() 方 法根據(jù)給定的 Bitmap 創(chuàng)建 一個新的,縮放后的 Bitmap。 * Shader.TileMode類型的參數(shù)包括CLAMP、MIRROR和REPEAT3個可選值,其中,CLAMP為使用 * 邊界顏色來填充剩余的空間;MIRROR為采用鏡像方式;REPEAT為采用重復(fù)方式*/ //圓形的drawable drawable=new ShapeDrawable(new OvalShape()); drawable.getPaint().setShader(shader); drawable.setBounds(0, 0, RADIUS*2, RADIUS*2);//設(shè)置圓的外切矩形 bitmap_magnifiter=BitmapFactory.decodeResource(getResources(), R.drawable.magnifiter);//獲取放大鏡圖像 m_left=RADIUS-bitmap_magnifiter.getWidth()/2;//計算放大鏡默認的左邊距 m_top=RADIUS-bitmap_magnifiter.getHeight()/2;//計算放大鏡默認的右邊距 } @Override protected void onDraw(Canvas canvas) { canvas.drawBitmap(bitmap, 0,0, null);//繪制背景圖像 canvas.drawBitmap(bitmap_magnifiter, m_left, m_top,null);//繪制放大鏡圖像 drawable.draw(canvas);//繪制放大后的圖像 super.onDraw(canvas); } //重寫onTouchEvent方法實現(xiàn)當用戶觸摸屏幕時,放大觸摸點附近的圖像 @Override public boolean onTouchEvent(MotionEvent event) { final int x=(int)event.getX(); final int y=(int)event.getY(); //平移到繪制shader的起始位置 matrix.setTranslate(RADIUS-x*FACTOR, RADIUS-y*FACTOR); drawable.getPaint().getShader().setLocalMatrix(matrix); drawable.setBounds(x-RADIUS,y-RADIUS,x+RADIUS,y+RADIUS);//設(shè)置圓的外切矩形 m_left=x-bitmap_magnifiter.getWidth()/2;//計算放大鏡的左邊距 m_top=y-bitmap_magnifiter.getHeight()/2;//計算放大鏡的右邊距 invalidate();//重繪畫布 return true; } } }
運行效果如開頭圖片顯示效果一樣,測試成功。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程基于自定義控件實現(xiàn)時鐘功能的方法
這篇文章主要介紹了Android編程基于自定義控件實現(xiàn)時鐘功能的方法,結(jié)合實例形式詳細分析了Android自定義控件的定義及時鐘功能相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-03-03Android JNI 調(diào)用時緩存字段和方法ID示例
這篇文章主要介紹了Android JNI 調(diào)用時緩存字段和方法ID示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07強制Android應(yīng)用使用某個Locale的方法
這篇文章主要介紹了強制Android應(yīng)用使用某個Locale的方法,涉及Android基于Locale進行語言設(shè)置的相關(guān)技巧,需要的朋友可以參考下2015-10-10在ubuntu下編譯ijkplayer-android的方法
下面小編就為大家分享一篇在ubuntu下編譯ijkplayer-android的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android自定義LocationMarker的實現(xiàn)詳解
這篇文章主要為大家詳細介紹一個比較簡單的東西:自定義繪制Marker 其實就是自定義view, 跟軌跡沒太多關(guān)聯(lián),感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02