android 圆角图片和灰度设置
[代码] [Java]代码
001import android.graphics.Bitmap;
002import android.graphics.BitmapFactory;
003import android.graphics.Canvas;
004import android.graphics.ColorMatrix;
005import android.graphics.ColorMatrixColorFilter;
006import android.graphics.Paint;
007import android.graphics.PorterDuffXfermode;
008import android.graphics.Rect;
009import android.graphics.RectF;
010import android.graphics.Bitmap.Config;
011import android.graphics.PorterDuff.Mode;
012import android.graphics.drawable.Drawable;
013import android.os.Bundle;
014import android.util.Log;
015import android.widget.Button;
016import android.widget.ImageView;
017import android.widget.TextView;
018
019public class ProfileActivity extends BaseActivity {
020private static final String TAG = ProfileActivity.class.getName();
021private static final int AVATAR_WHIDTH = 250;
022private static final int AVATAR_HIGHT = 180;
023final float roundPx = 12;
024
025private ImageView people_avatar;
026private ImageView people_star;
027private TextView people_name;
028private TextView people_listeners;
029private TextView people_similar;
030private TextView people_tweer;
031private Button people_button;
032
033private Bitmap people_user_avatar_bitmap;
034private Drawable people_user_avatar;
035private Drawable star_strut_on;
036private Drawable star_strut_off;
037
038// ----------------------------test data------------------
039private int temp_many_int = 231;
040
041@Override
042protected void onCreate(Bundle savedInstanceState) {
043super.onCreate(savedInstanceState);
044Log.v(TAG, "onCreate ....");
045setContentView(R.layout.people_profile);
046
047initView(false);
048
049}
050
051/***
052* 加载布局元素 isOnline 是否在线如果在线就原图,否则灰度调整
053*/
054private void initView(boolean isOnline) {
055people_avatar = (ImageView) findViewById(R.id.profile_avatar);
056people_name = (TextView) findViewById(R.id.profile_name);
057people_listeners = (TextView) findViewById(R.id.profile_listeners);
058people_similar = (TextView) findViewById(R.id.profile_similar);
059people_tweer = (TextView) findViewById(R.id.profile_tweers);
060people_star = (ImageView)findViewById(R.id.prifile_star);
061people_button = (Button)findViewById(R.id.profile_button);
062people_button.setText(getString(R.string.people_play));
063// -----------------------补充模拟数据---------------------
064people_name.setText("kiki");
065people_listeners.setText(getString(R.string.people_many_listenters,new String[] { temp_many_int + "" }));
066people_similar.setText(this.getString(R.string.people_similar));
067people_tweer.setText(getString(R.string.people_tweers));
068people_user_avatar = this.getResources().getDrawable(R.drawable.xyx);
069people_star.setImageResource(R.drawable.btn_star_big_buttonless_on);
070
071
072people_user_avatar_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background);
073Bitmap output = Bitmap.createBitmap(AVATAR_WHIDTH, AVATAR_HIGHT, Config.ARGB_8888);
074Canvas canvas = new Canvas(output);
075final int color = 0xff424242;
076final Paint paint = new Paint();
077final Rect rect = new Rect(0, 0, AVATAR_WHIDTH, AVATAR_HIGHT);
078final RectF rectF = new RectF(0, 0, AVATAR_WHIDTH, AVATAR_HIGHT);
079paint.setAntiAlias(true);
080canvas.drawARGB(0, 0, 0, 0);
081paint.setDither(true); // 获取跟清晰的图像采样
082
083if (!isOnline) { //灰度调整
084people_button.setText(getString(R.string.people_follow));
085ColorMatrix cm = new ColorMatrix();
086cm.setSaturation(0);
087ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
088paint.setColorFilter(f);
089people_star.setImageResource(R.drawable.btn_star_big_buttonless_off);
090}
091paint.setColor(color);
092canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
093paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
094canvas.drawBitmap(people_user_avatar_bitmap, rect, rect, paint);
095people_avatar.setImageBitmap(output);
096
097}
098
099@Override
100protected void onDestroy() {
101super.onDestroy();
102Log.v(TAG, "onDestroy ....");
103}
104
105@Override
106protected void onPause() {
107super.onPause();
108Log.v(TAG, "onPause ....");
109}
110
111@Override
112protected void onRestart() {
113super.onRestart();
114Log.v(TAG, "onRestart ....");
115}
116
117@Override
118protected void onResume() {
119super.onResume();
120Log.v(TAG, "onResume ....");
121}
122
123@Override
124protected void onStart() {
125super.onStart();
126Log.v(TAG, "onStart ....");
127}
128
129@Override
130protected void onStop() {
131super.onStop();
132Log.v(TAG, "onStop ....");
133}
134
135}