addison_lin 发表于 2012-2-1 11:45:27

android 圆角图片和灰度设置

[代码] 代码
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class ProfileActivity extends BaseActivity {
private static final String TAG = ProfileActivity.class.getName();
private static final int AVATAR_WHIDTH = 250;
private static final int AVATAR_HIGHT = 180;
final float roundPx = 12;
private ImageView people_avatar;
private ImageView people_star;
private TextView people_name;
private TextView people_listeners;
private TextView people_similar;
private TextView people_tweer;
private Button   people_button;
private Bitmap people_user_avatar_bitmap;
private Drawable people_user_avatar;
private Drawable star_strut_on;
private Drawable star_strut_off;
// ----------------------------test data------------------
private int temp_many_int = 231;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate ....");
setContentView(R.layout.people_profile);
initView(false);
}
/***
* 加载布局元素 isOnline 是否在线如果在线就原图,否则灰度调整
*/
private void initView(boolean isOnline) {
people_avatar   = (ImageView) findViewById(R.id.profile_avatar);
people_name   = (TextView) findViewById(R.id.profile_name);
people_listeners = (TextView) findViewById(R.id.profile_listeners);
people_similar   = (TextView) findViewById(R.id.profile_similar);
people_tweer   = (TextView) findViewById(R.id.profile_tweers);
people_star   = (ImageView)findViewById(R.id.prifile_star);
people_button    = (Button)findViewById(R.id.profile_button);
people_button.setText(getString(R.string.people_play));
// -----------------------补充模拟数据---------------------
people_name.setText("kiki");
people_listeners.setText(getString(R.string.people_many_listenters, new String[] { temp_many_int + "" }));
people_similar.setText(this.getString(R.string.people_similar));
people_tweer.setText(getString(R.string.people_tweers));
people_user_avatar = this.getResources().getDrawable(R.drawable.xyx);
people_star.setImageResource(R.drawable.btn_star_big_buttonless_on);

people_user_avatar_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background);
Bitmap output = Bitmap.createBitmap(AVATAR_WHIDTH, AVATAR_HIGHT, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, AVATAR_WHIDTH, AVATAR_HIGHT);
final RectF rectF = new RectF(0, 0, AVATAR_WHIDTH, AVATAR_HIGHT);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setDither(true); // 获取跟清晰的图像采样
if (!isOnline) {   //灰度调整
people_button.setText(getString(R.string.people_follow));
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
people_star.setImageResource(R.drawable.btn_star_big_buttonless_off);
}
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(people_user_avatar_bitmap, rect, rect, paint);
people_avatar.setImageBitmap(output);
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v(TAG, "onDestroy ....");
}
@Override
protected void onPause() {
super.onPause();
Log.v(TAG, "onPause ....");
}
@Override
protected void onRestart() {
super.onRestart();
Log.v(TAG, "onRestart ....");
}
@Override
protected void onResume() {
super.onResume();
Log.v(TAG, "onResume ....");
}
@Override
protected void onStart() {
super.onStart();
Log.v(TAG, "onStart ....");
}
@Override
protected void onStop() {
super.onStop();
Log.v(TAG, "onStop ....");
}
}

ad189y 发表于 2013-2-6 18:39:51

关注一下,备用
哈哈
:handshake
页: [1]
查看完整版本: android 圆角图片和灰度设置