国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

簡(jiǎn)單實(shí)現(xiàn)Android放大鏡效果

2019-10-22 18:18:41
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

最終效果如圖

Android,放大鏡

具體實(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)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 昌平区| 四平市| 虎林市| 密云县| 杨浦区| 宁阳县| 青铜峡市| 衡阳市| 沈阳市| 宾阳县| 溆浦县| 左贡县| 浠水县| 大渡口区| 苏尼特右旗| 天津市| 昌都县| 呈贡县| 新蔡县| 通榆县| 东乌珠穆沁旗| 砀山县| 凤山市| 漯河市| 禄劝| 德兴市| 什邡市| 布拖县| 治多县| 鲜城| 龙岩市| 贺州市| 樟树市| 岑溪市| 团风县| 荥阳市| 亚东县| 焦作市| 普定县| 河曲县| 苏尼特左旗|