Android中怎么實現一個放大鏡效果,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
具體實現:
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,在文件中創建名為MyView的內部類,繼承android.view.View類,并添加構造方法和重寫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;//放大倍數 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);//創建BitmapShader對象 /* 注:Bitmap.createScaledBitmap() 方 法根據給定的 Bitmap 創建 一個新的,縮放后的 Bitmap。 * Shader.TileMode類型的參數包括CLAMP、MIRROR和REPEAT3個可選值,其中,CLAMP為使用 * 邊界顏色來填充剩余的空間;MIRROR為采用鏡像方式;REPEAT為采用重復方式*/ //圓形的drawable drawable=new ShapeDrawable(new OvalShape()); drawable.getPaint().setShader(shader); drawable.setBounds(0, 0, RADIUS*2, RADIUS*2);//設置圓的外切矩形 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方法實現當用戶觸摸屏幕時,放大觸摸點附近的圖像 @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);//設置圓的外切矩形 m_left=x-bitmap_magnifiter.getWidth()/2;//計算放大鏡的左邊距 m_top=y-bitmap_magnifiter.getHeight()/2;//計算放大鏡的右邊距 invalidate();//重繪畫布 return true; } } }
看完上述內容,你們掌握Android中怎么實現一個放大鏡效果的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。