android講義2之簡單圖片瀏覽器且有截圖效果(對ImageView使用)

來源:互聯網
上載者:User

布局特別之處:

//與上一控制項底部的距離

android:layout_marginTop="10dp"

代碼階段分析:

//返回可畫的視圖,並用向下轉型為Bitmap的可畫視圖

BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();

//如果圖片還未回收,先強制回收該圖片

if (!bitmapDrawable.getBitmap().isRecycled())
{
bitmapDrawable.getBitmap().recycle();
}

//改變ImageView顯示的圖片
image1.setImageBitmap(BitmapFactory.decodeResource(getResources(), images[++currentImg]));

//截取原來的圖片進行顯示

1.使用setOnTouchListener監聽

2.獲得螢幕的寬,用於第三步的取得實際比例

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
System.out.println(dm.widthPixels);

3.獲得bitmap圖片實際大小與第一個ImageView的縮放比例

double scale = bitmap.getWidth() /dm.widthPixels;

4.擷取需要顯示的圖片的開始點

int x = (int) (event.getX() * scale);
int y = (int) (event.getY() * scale);

5.演算法規定開始點加上顯示地區不能大於圖片地區

if (x + 120 > bitmap.getWidth())
{
x = bitmap.getWidth() - 120;
}
if (y + 120 > bitmap.getHeight())
{
y = bitmap.getHeight() - 120;
}

6.顯示圖片的指定地區
image2.setImageBitmap(Bitmap.createBitmap(bitmap, x, y, 120, 120));

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.