Android仿QQ討論組頭像效果
本文實(shí)例為大家分享了Android仿QQ討論組頭像展示的具體代碼,供大家參考,具體內(nèi)容如下
一、效果圖

二、實(shí)現(xiàn)
基本實(shí)現(xiàn)過(guò)程:
1.將原圖片讀取為bitmap
2.在Canvas畫(huà)布上計(jì)算出圖片位置,并繪制新的圖片。
(ps:計(jì)算位置對(duì)我來(lái)說(shuō)是難點(diǎn),花了好長(zhǎng)時(shí)間);
三、源碼
1.布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" android:background="#AEAEAE" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <ImageView android:id="@+id/img_src_0" android:layout_width="0dp" android:layout_height="match_parent" android:padding="5dp" android:layout_weight="1" android:adjustViewBounds="true" /> <ImageView android:id="@+id/img_src_1" android:layout_width="0dp" android:layout_height="match_parent" android:padding="5dp" android:layout_weight="1" android:adjustViewBounds="true" /> <ImageView android:id="@+id/img_src_2" android:layout_width="0dp" android:layout_height="match_parent" android:padding="5dp" android:layout_weight="1" android:adjustViewBounds="true" /> <ImageView android:id="@+id/img_src_3" android:layout_width="0dp" android:layout_height="match_parent" android:padding="5dp" android:layout_weight="1" android:adjustViewBounds="true" /> <ImageView android:id="@+id/img_src_4" android:layout_width="0dp" android:layout_height="match_parent" android:padding="5dp" android:layout_weight="1" android:adjustViewBounds="true" /> </LinearLayout> <ImageView android:id="@+id/img_result_2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:id="@+id/img_result_3" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:id="@+id/img_result_4" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:id="@+id/img_result_5" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
2.Activity
package com.hand.px_demo.QQHead;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import com.hand.px_demo.R;
import java.util.ArrayList;
/**
* Created by panx on 2016/12/12.
*/
public class QHeadActivity extends Activity{
private ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
private ImageView img_src_0;
private ImageView img_src_1;
private ImageView img_src_2;
private ImageView img_src_3;
private ImageView img_src_4;
private ImageView img_result_2;
private ImageView img_result_3;
private ImageView img_result_4;
private ImageView img_result_5;
private HeadManager headManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qq_head);
headManager = new HeadManager();
initView();
initData();
}
private void initView(){
img_src_0 = (ImageView)findViewById(R.id.img_src_0);
img_src_1 = (ImageView)findViewById(R.id.img_src_1);
img_src_2 = (ImageView)findViewById(R.id.img_src_2);
img_src_3 = (ImageView)findViewById(R.id.img_src_3);
img_src_4 = (ImageView)findViewById(R.id.img_src_4);
img_result_2 = (ImageView)findViewById(R.id.img_result_2);
img_result_3 = (ImageView)findViewById(R.id.img_result_3);
img_result_4 = (ImageView)findViewById(R.id.img_result_4);
img_result_5 = (ImageView)findViewById(R.id.img_result_5);
}
private void initData(){
//從drawable中獲取圖片資源的bitmap
Resources r = this.getResources();
Bitmap bitmap0 = BitmapFactory.decodeResource(r,R.drawable.src_head_1);
Bitmap bitmap1 = BitmapFactory.decodeResource(r,R.drawable.src_head_2);
Bitmap bitmap2 = BitmapFactory.decodeResource(r,R.drawable.src_head_3);
Bitmap bitmap3 = BitmapFactory.decodeResource(r,R.drawable.src_head_4);
Bitmap bitmap4 = BitmapFactory.decodeResource(r,R.drawable.src_head_5);
bitmaps.add(bitmap0);
bitmaps.add(bitmap1);
bitmaps.add(bitmap2);
bitmaps.add(bitmap3);
bitmaps.add(bitmap4);
img_src_0.setImageBitmap(bitmap0);
img_src_1.setImageBitmap(bitmap1);
img_src_2.setImageBitmap(bitmap2);
img_src_3.setImageBitmap(bitmap3);
img_src_4.setImageBitmap(bitmap4);
Bitmap r2Bitmap = headManager.getBitMap(bitmaps, 2);
Bitmap r3Bitmap = headManager.getBitMap(bitmaps, 3);
Bitmap r4Bitmap = headManager.getBitMap(bitmaps, 4);
Bitmap r5Bitmap = headManager.getBitMap(bitmaps, 5);
img_result_2.setImageBitmap(r2Bitmap);
img_result_3.setImageBitmap(r3Bitmap);
img_result_4.setImageBitmap(r4Bitmap);
img_result_5.setImageBitmap(r5Bitmap);
}
}
3.繪制圖片的核心代碼
(1)壓縮圖片
/*對(duì)原始圖片進(jìn)行縮放*/
public Bitmap zoomImage(Bitmap bgimage, double newWidth,
double newHeight) {
// 獲取這個(gè)圖片的寬和高
float width = bgimage.getWidth();
float height = bgimage.getHeight();
// 創(chuàng)建操作圖片用的matrix對(duì)象
Matrix matrix = new Matrix();
// 計(jì)算寬高縮放率
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 縮放圖片動(dòng)作
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
(int) height, matrix, true);
return bitmap;
}
(2)將圖片改成圓角
private Bitmap makeRoundCorner(Bitmap bitmap, int width, int height) {
int left = 0, top = 0, right = width, bottom = height;
float roundPx = height / 2;
if (width > height) {
left = (width - height) / 2;
top = 0;
right = left + height;
bottom = height;
} else if (height > width) {
left = 0;
top = (height - width) / 2;
right = width;
bottom = top + width;
roundPx = width / 2;
}
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int color = 0xff424242;
Paint paint = new Paint();
Rect rect = new Rect(left, top, right, bottom);
RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
(3)獲取繪制的坐標(biāo)
private ArrayList<Point> getFivePoints(int point_x, int point_y, int d) {
ArrayList<Point> points = new ArrayList<Point>();
//圓心至圖片的距離
int wr = point_y - d;
//大圓心至小圓心的距離
int R = wr + d / 2;
//頭像半徑,頭像直徑為d
int r = d / 2;
int RCos18 = (int) (R * (Math.cos(0.1 * Math.PI)));
int RSin18 = (int) (R * (Math.sin(0.1 * Math.PI)));
int RCos36 = (int) (R * (Math.cos(0.2 * Math.PI)));
int RSin36 = (int) (R * (Math.sin(0.2 * Math.PI)));
Point point1 = new Point(point_x - r, 0);
Point point2 = new Point();
point2.x = RCos18 + point_x - r;
point2.y = point_y - RSin18 - r;
Point point3 = new Point();
point3.x = RSin36 + point_x - r;
point3.y = point_y + RCos36 - r;
Point point4 = new Point();
point4.x = point_x - RSin36 - r;
point4.y = point3.y;
Point point5 = new Point();
point5.x = point_x - (int) (RCos18) - r;
point5.y = point2.y;
points.add(point1);
points.add(point2);
points.add(point3);
points.add(point4);
points.add(point5);
return points;
}
(4)該類(lèi)全部源碼
package com.hand.px_demo.QQHead;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import com.hand.px_demo.R;
import java.util.ArrayList;
/**
* Created by panx on 2016/12/12.
*/
public class QHeadActivity extends Activity{
private ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
private ImageView img_src_0;
private ImageView img_src_1;
private ImageView img_src_2;
private ImageView img_src_3;
private ImageView img_src_4;
private ImageView img_result_2;
private ImageView img_result_3;
private ImageView img_result_4;
private ImageView img_result_5;
private HeadManager headManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qq_head);
headManager = new HeadManager();
initView();
initData();
}
private void initView(){
img_src_0 = (ImageView)findViewById(R.id.img_src_0);
img_src_1 = (ImageView)findViewById(R.id.img_src_1);
img_src_2 = (ImageView)findViewById(R.id.img_src_2);
img_src_3 = (ImageView)findViewById(R.id.img_src_3);
img_src_4 = (ImageView)findViewById(R.id.img_src_4);
img_result_2 = (ImageView)findViewById(R.id.img_result_2);
img_result_3 = (ImageView)findViewById(R.id.img_result_3);
img_result_4 = (ImageView)findViewById(R.id.img_result_4);
img_result_5 = (ImageView)findViewById(R.id.img_result_5);
}
private void initData(){
//從drawable中獲取圖片資源的bitmap
Resources r = this.getResources();
Bitmap bitmap0 = BitmapFactory.decodeResource(r,R.drawable.src_head_1);
Bitmap bitmap1 = BitmapFactory.decodeResource(r,R.drawable.src_head_2);
Bitmap bitmap2 = BitmapFactory.decodeResource(r,R.drawable.src_head_3);
Bitmap bitmap3 = BitmapFactory.decodeResource(r,R.drawable.src_head_4);
Bitmap bitmap4 = BitmapFactory.decodeResource(r,R.drawable.src_head_5);
bitmaps.add(bitmap0);
bitmaps.add(bitmap1);
bitmaps.add(bitmap2);
bitmaps.add(bitmap3);
bitmaps.add(bitmap4);
img_src_0.setImageBitmap(bitmap0);
img_src_1.setImageBitmap(bitmap1);
img_src_2.setImageBitmap(bitmap2);
img_src_3.setImageBitmap(bitmap3);
img_src_4.setImageBitmap(bitmap4);
Bitmap r2Bitmap = headManager.getBitMap(bitmaps, 2);
Bitmap r3Bitmap = headManager.getBitMap(bitmaps, 3);
Bitmap r4Bitmap = headManager.getBitMap(bitmaps, 4);
Bitmap r5Bitmap = headManager.getBitMap(bitmaps, 5);
img_result_2.setImageBitmap(r2Bitmap);
img_result_3.setImageBitmap(r3Bitmap);
img_result_4.setImageBitmap(r4Bitmap);
img_result_5.setImageBitmap(r5Bitmap);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android一步步帶你在RecyclerView上面實(shí)現(xiàn)"拖放"和"滑動(dòng)刪除"功能
這篇文章主要介紹了Android一步步帶你在RecyclerView上面實(shí)現(xiàn)"拖放"和"滑動(dòng)刪除"功能,需非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-03-03
Kotlin Lambda表達(dá)式實(shí)踐使用介紹
lambda 本質(zhì)上是可以傳遞給函數(shù)的一小段代碼,Kotlin 與 Java 中的 Lambda 有一定的區(qū)別,除了對(duì) lambda 的全面支持外,還有內(nèi)聯(lián)函數(shù)等簡(jiǎn)潔高效的特性。下面我們來(lái)仔細(xì)看一下2022-12-12
Android TextSwitcher實(shí)現(xiàn)文字上下翻牌效果(銅板街)
這篇文章主要介紹了Android TextSwitcher實(shí)現(xiàn)文字上下翻牌效果(銅板街),需要的朋友可以參考下2017-05-05
Android 利用廣播監(jiān)聽(tīng)usb連接狀態(tài)(變化情況)
這篇文章主要介紹了Android 利用廣播監(jiān)聽(tīng)usb連接狀態(tài),需要的朋友可以參考下2017-06-06
Android Studio 安裝配置方法完整教程【小白秒懂】
這篇文章主要介紹了Android Studio 安裝配置方法完整教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-03-03
Android序列化實(shí)現(xiàn)接口Serializable與Parcelable詳解
我們使用 Intent 傳遞數(shù)據(jù)的時(shí)候,putExtra() 所支持的數(shù)據(jù)類(lèi)型事有限的,當(dāng)需要傳遞自定義對(duì)象的時(shí)候就需要序列化。Serializable更簡(jiǎn)單但是會(huì)把整個(gè)對(duì)象進(jìn)行序列化因此效率比Parcelable低一些2022-12-12
Android 連接藍(lán)牙掃碼器無(wú)輸入框的實(shí)現(xiàn)
這篇文章主要介紹了Android 連接藍(lán)牙掃碼器無(wú)輸入框的實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02

