利用之前學(xué)過(guò)的圖形圖像繪畫(huà)技術(shù)和圖片添加特效技術(shù),我們來(lái)實(shí)現(xiàn)一個(gè)Android放大鏡的簡(jiǎn)單應(yīng)用。
最終效果如圖

具體實(shí)現(xiàn):
用來(lái)顯示自定義的繪圖類(lèi)的布局文件
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>
打開(kāi)MainActivity,在文件中創(chuàng)建名為MyView的內(nèi)部類(lèi),繼承android.view.View類(lèi),并添加構(gòu)造方法和重寫(xiě)onDraw(Canvas canvas)方法,在里面進(jìn)行作圖:
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對(duì)象 /* 注:Bitmap.createScaledBitmap() 方 法根據(jù)給定的 Bitmap 創(chuàng)建 一個(gè)新的,縮放后的 Bitmap。 * Shader.TileMode類(lèi)型的參數(shù)包括CLAMP、MIRROR和REPEAT3個(gè)可選值,其中,CLAMP為使用 * 邊界顏色來(lái)填充剩余的空間;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;//計(jì)算放大鏡默認(rèn)的左邊距 m_top=RADIUS-bitmap_magnifiter.getHeight()/2;//計(jì)算放大鏡默認(rèn)的右邊距 } @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); } //重寫(xiě)onTouchEvent方法實(shí)現(xiàn)當(dāng)用戶(hù)觸摸屏幕時(shí),放大觸摸點(diǎ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;//計(jì)算放大鏡的左邊距 m_top=y-bitmap_magnifiter.getHeight()/2;//計(jì)算放大鏡的右邊距 invalidate();//重繪畫(huà)布 return true; } } } 運(yùn)行效果如開(kāi)頭圖片顯示效果一樣,測(cè)試成功。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注