1. 程式人生 > >android: RoundCornerImageView 圖片檢視 倒圓角

android: RoundCornerImageView 圖片檢視 倒圓角

需求:固定大小的ImageView,但是大小不固定的ImageBitmap,想要使用這個大小不一的ImageBitmap填充滿這個ImageView,從而讓整個檢視看起來整齊

這個可以直接在layout.xml這樣實現的

<ImageView android:id="@+id/foodItem_foodImg"
		android:src="@drawable/food_img_na"
		android:layout_width="277dip"
		android:layout_height="300dip"
		android:scaleType="centerCrop"
	/>

這裡邊的centerCrop的意思就是:Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). 英語撇,就不翻譯了,見諒。

但是這樣的圖片看起來是有直角的,是生硬的,給使用者一種強硬的感覺。

如果我們把圖片的4個角都給他倒圓角,就看起來圓潤一些,不至於那麼傷人,那麼生硬。

但是我想了很久,也找了很久,也沒有找到直接操作ImageBitmap來實現:centerCrop效果 + 圓角效果

後來想到直接在在ImageView.onDraw的時候,給他畫出圓角來,這樣這個ImageView裡面的ImageBitmap也有圓角的效果了

程式碼是這樣實現的

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RoundCornerImageView extends ImageView {

	public RoundCornerImageView(Context context) {
		super(context);
	}
	
	public RoundCornerImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public RoundCornerImageView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Path clipPath = new Path();
		int w = this.getWidth();
		int h = this.getHeight();
		clipPath.addRoundRect(new RectF(0, 0, w, h), 10.0f, 10.0f, Path.Direction.CW);
		canvas.clipPath(clipPath);
		super.onDraw(canvas);
	}
}

下面這行程式碼就能畫出圓角效果,10度的角
clipPath.addRoundRect(new RectF(0, 0, w, h), 10.0f, 10.0f, Path.Direction.CW);

在layout.xml中直接使用RoundCornerImageView就會出現圓角的ImageView效果

<cn.hus.app.ui.utils.RoundCornerImageView android:id="@+id/foodItem_foodImg"
		android:src="@drawable/food_img_na"
		android:layout_width="277dip"
		android:layout_height="300dip"
		android:scaleType="centerCrop"
	/>


使用請謹慎,本例沒有考慮效能方面的東西,謹慎再謹慎。

相關推薦

android: RoundCornerImageView 圖片檢視 圓角

需求:固定大小的ImageView,但是大小不固定的ImageBitmap,想要使用這個大小不一的ImageBitmap填充滿這個ImageView,從而讓整個檢視看起來整齊 這個可以直接在layout.xml這樣實現的 <ImageView android:id="

android網路圖片檢視

package com.itheima74.internetpicturelook; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; impo

Android RecyclerView中用fresco展示圓角圖片,點選檢視詳情(或原生解析)

package com.example.earl.lianxilianxi; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.support.v7.wi

Android核心技術-day04-01-網路圖片檢視器(安卓訊息迴圈機制)

package com.gaozewen.netimageviewer; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import and

Android 圖片檢視與選擇

Android 好用的框架與UI效果demo收集 1.RecyclerViewCardGallery RecyclerView實現迴圈banner,替代ViewPager方案。能夠快速滑動並最終定位到居中位置(相比於原庫支援了迴圈滑動) github https://git

Android 圖片繪製成圓角圖片

import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Matrix; import androi

Android WebView 圖片超出寬度自適應,點選檢視大圖

webView 配置  WebSettings webSettings = webView.getSettings(); webSettings.setJava

Xamarin.Android ImageView 圖片圓角顯示

第一步:在 values 資料夾下新增 Attrs.xml 檔案 <?xml version="1.0" encoding="utf-8" ?> <resources> <declare-styleable name="RoundImageView">

Android 實現WebView點選圖片檢視大圖列表及圖片儲存

在日常開發過程中,有時候會遇到需要在app中嵌入網頁,此時使用WebView實現效果,但在預設情況下是無法點選圖片檢視大圖的,更無法儲存圖片。本文將就這一系列問題的實現進行說明。 圖示: 專案的知識點: 載入網頁後如何捕捉網頁中的圖片點選事件; 獲取點選

Android開發之自定義圓角矩形圖片ImageView

android中的ImageView只能顯示矩形的圖片,這樣一來不能滿足我們其他的需求,比如要顯示圓角矩形的圖片,這個時候,我們就需要自定義ImageView了,其原理就是首先獲取到圖片的Bitmap,然後進行裁剪對應的圓角矩形的bitmap,然後在onDraw()進行繪製

Fresco 圖片圓角實現原理及 Android圖片圓角實現方法

上篇文章 介紹了 Fresco 基礎使用和實現圖片圓角的方法,可以通過兩種方式來實現圓角:BITMAP_ONLY 模式和 OVERLAY_COLOR 模式。本文通過分析 Fresco 原始碼來介紹這兩種方式實現圓角的原理,並總結 Android 中常用的實現圖

Android】將圖片轉換為圓角

摘自:https://github.com/NashLegend/SourceWall.git import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.

Android相簿支援檢視本地相簿,點選放大圖片,滑動切換圖片,手勢放大縮小

最近在做一個專案,需要用到點選選擇相簿圖片和展示好友說說圖片,之前也未接觸過,找了很久,都沒有找到我想要的效果,不是隻能檢視相簿點選放大,要不就只是左右滑動的demo,於是修改了兩個大神的程式碼(來源

android實現圖片圓角化實現三種方法

方法一 自定義ImageView /** * 自定義的圓角矩形ImageView,可以直接當元件在佈局中使用。 * @author caizhiming * */ public class

Android開發之網路圖片檢視方法BitmapFactory.decodeStream()學習01

Android實現網路圖片的檢視有好幾種方法,但這本身是一種很耗時的操作,可以通過直接獲取和操作執行緒的方法,自己學習使用了BitmapFactory.decodeStream()在程式碼中自己寫了註釋,大致分為以下幾步: ①設定URL物件和連線  ②設定一下在連線和讀取過

android如何給整個檢視view圓角顯示

關於如何給一個view設定圓角,有哪些方法呢? 1.給該view設定一個圓角的背景,這是最常見的用法。如以下程式碼:round_bg.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns

Android 圖片檢視器實現 PhotoView + ViewPager

今天看了今日頭條APP的圖片檢視的效果,就自己實現了一下。效果如下首先引入兩個庫一個是圖片縮放的功能:PhotoView框架compile 'com.github.chrisbanes:PhotoView:1.2.6'一個介面滑動退出效果(比如自己滑動空白區域,就退出介面,而

android studio for android learning (十三) 一個簡單的圖片檢視器示例

1、程式介面很簡單,一個按鈕和一個ImageView元件,當用戶按下按鈕時,程式會自動搜尋/assets/目錄下的一張圖片。 2.在android studio中新建的工程並不存在assets目錄

Android之---RecycleView實現簡單的圖片檢視功能(類似相簿檢視器)

RecycleView實現簡單的圖片檢視功能(類似相簿檢視器) 效果圖 自定義PreviewRecycleView繼承自RecycleView public class PreviewRecycleView extends Recycler

Android 兩種製作圓形/圓角圖片的方法

前言: 目前網上有很多圓角圖片的例項,Github上也有一些成熟的專案。之前做專案,為了穩定高效都是選用Github上的專案直接用。但這種結束也是Android開發必備技能 ,所以今天就來簡單研究一下該技術,分享給大家。 預備知識: Xfermode介紹: 下面是Andr