public class ImageButtonClickUtils {
private ImageButtonClickUtils(){
}
/**
* 设置按钮的正反选效果
*
* */
public static void setClickState(View view, final int normalResId, final int pressResId){
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:{
v.setBackgroundResource(pressResId);
}
break;
case MotionEvent.ACTION_MOVE:{
v.setBackgroundResource(pressResId);
}
break;
case MotionEvent.ACTION_UP:{
v.setBackgroundResource(normalResId);
}
break;
default:{
}
break;
}
// 为了不影响监听按钮的onClick回调,返回值应为false
return false;
}
});
}
}
ImageButton personalInfoBtn = (ImageButton)findViewById(R.id.personalBtnId);
personalInfoBtn.setOnTouchListener(new OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN){
((ImageButton)v).setColorFilter(getResources().getColor(0X50000000));
}else if(action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL){
((ImageButton)v).clearColorFilter();
}
// 为了不影响监听按钮的onClick回调,返回值应为false
return false;
}
});
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.eebbk.hanziLearning.activity.R;
public class RootSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable{
private float mViewWidth = 0;
private float mViewHeight = 0;
private int mResourceId = 0;
private Context mContext = null;
private volatile boolean isRunning = false;
private SurfaceHolder mSurfaceHolder = null;
public RootSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initRootSurfaceView(context, attrs, defStyleAttr, 0);
}
public RootSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
initRootSurfaceView(context, attrs, 0, 0);
}
private void initRootSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
mContext = context;
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RootSurfaceView, defStyleAttr, defStyleRes);
int n = a.getIndexCount();
mViewWidth = displayMetrics.widthPixels;
mViewHeight = displayMetrics.heightPixels;
for(int index=0; index<n; index++){
int attr = a.getIndex(index);
switch(attr){
case R.styleable.RootSurfaceView_background:{
mResourceId = a.getResourceId(attr, 0);
}
break;
case R.styleable.RootSurfaceView_view_width:{
mViewWidth = a.getDimension(attr, displayMetrics.widthPixels);
}
break;
case R.styleable.RootSurfaceView_view_height:{
mViewHeight = a.getDimension(attr, displayMetrics.heightPixels);
}
break;
default:{
}
break;
}
}
a.recycle();
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
}
private Bitmap getDrawBitmap(Context context, float width, float height) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mResourceId);
Bitmap resultBitmap = zoomImage(bitmap, width, height);
return resultBitmap;
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
System.out.println("RootSurfaceView surfaceChanged");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
drawBackGround(holder);
System.out.println("RootSurfaceView surfaceCreated");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isRunning = false;
System.out.println("RootSurfaceView surfaceDestroyed");
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
System.out.println("RootSurfaceView onAttachedToWindow");
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
System.out.println("RootSurfaceView onDetachedFromWindow");
}
@Override
public void run(){
while(isRunning){
synchronized (mSurfaceHolder) {
if(!mSurfaceHolder.getSurface().isValid()){
continue;
}
drawBackGround(mSurfaceHolder);
}
isRunning = false;
break;
}
}
private void drawBackGround(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas();
Bitmap bitmap = getDrawBitmap(mContext, mViewWidth, mViewHeight);
canvas.drawBitmap(bitmap, 0, 0, null);
bitmap.recycle();
holder.unlockCanvasAndPost(canvas);
}
public static Bitmap zoomImage( Bitmap bgimage , float newWidth , float newHeight ) {
float width = bgimage.getWidth( );
float height = bgimage.getHeight( );
Matrix matrix = new Matrix();
float scaleWidth = newWidth/width;
float scaleHeight = newHeight/height;
matrix.postScale( scaleWidth, scaleHeight );
Bitmap bitmap = Bitmap.createBitmap( bgimage, 0, 0, ( int ) width , ( int ) height, matrix, true );
if( bitmap != bgimage ){
bgimage.recycle();
bgimage = null;
}
return bitmap;
}
}
<declare-styleable name="RootSurfaceView"> <attr name="background" format="reference" /> <attr name="view_width" format="dimension" /> <attr name="view_height" format="dimension" /> </declare-styleable>
<!-- 设置界面 --> <activity android:name=".SettingActivity" android:hardwareAccelerated="false" android:screenOrientation="sensorLandscape" android:theme="@style/Translucent_NoTitle"> </activity>
// view.setLayerType || 在定义view的构造方法中调用该方法 setLayerType(View.LAYER_TYPE_SOFTWARE, null);
/**
* 释放AnimationDrawable占用的内存
*
*
* */
@SuppressWarnings("unused")
private void freeAnimationDrawable(AnimationDrawable animationDrawable) {
animationDrawable.stop();
for (int i = 0; i < animationDrawable.getNumberOfFrames(); ++i){
Drawable frame = animationDrawable.getFrame(i);
if (frame instanceof BitmapDrawable) {
((BitmapDrawable)frame).getBitmap().recycle();
}
frame.setCallback(null);
}
animationDrawable.setCallback(null);
}
/**
* 图片动态切换器
*
* */
public class AnimImageView {
private static final int MSG_START = 0xf1;
private static final int MSG_STOP = 0xf2;
private static final int STATE_STOP = 0xf3;
private static final int STATE_RUNNING = 0xf4;
/* 运行状态*/
private int mState = STATE_RUNNING;
private ImageView mImageView;
/* 图片资源ID列表*/
private List<Integer> mResourceIdList = null;
/* 定时任务*/
private Timer mTimer = null;
private AnimTimerTask mTimeTask = null;
/* 记录播放位置*/
private int mFrameIndex = 0;
/* 播放形式*/
private boolean isLooping = false;
public AnimImageView( ){
mTimer = new Timer();
}
/**
* 设置动画播放资源
*
* */
public void setAnimation( HanziImageView imageview, List<Integer> resourceIdList ){
mImageView = imageview;
mResourceIdList = resourceIdList;
}
/**
* 开始播放动画
* @param loop 时候循环播放
* @param duration 动画播放时间间隔
* */
public void start(boolean loop, int duration){
stop();
isLooping = loop;
mFrameIndex = 0;
mState = STATE_RUNNING;
mTimeTask = new AnimTimerTask( );
mTimer.schedule(mTimeTask, 0, duration);
}
/**
* 停止动画播放
*
* */
public void stop(){
if (mTimeTask != null) {
mFrameIndex = 0;
mState = STATE_STOP;
mTimer.purge();
mTimeTask.cancel();
mTimeTask = null;
mImageView.setBackgroundResource(0);
}
}
/**
* 定时器任务
*
*
*/
class AnimTimerTask extends TimerTask {
@Override
public void run() {
if(mFrameIndex < 0 || mState == STATE_STOP){
return;
}
if( mFrameIndex < mResourceIdList.size() ){
Message msg = AnimHanlder.obtainMessage(MSG_START,0,0,null);
msg.sendToTarget();
}else{
mFrameIndex = 0;
if(!isLooping){
Message msg = AnimHanlder.obtainMessage(MSG_STOP,0,0,null);
msg.sendToTarget();
}
}
}
}
private Handler AnimHanlder = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MSG_START:{
if(mFrameIndex >=0 && mFrameIndex < mResourceIdList.size() && mState == STATE_RUNNING){
mImageView.setImageResource(mResourceIdList.get(mFrameIndex));
mFrameIndex++;
}
}
break;
case MSG_STOP:{
if (mTimeTask != null) {
mFrameIndex = 0;
mTimer.purge();
mTimeTask.cancel();
mState = STATE_STOP;
mTimeTask = null;
mImageView.setImageResource(0);
}
}
break;
default:
break;
}
}
};
}
<!--千万不要在主题中为Activity设置默认背景 <style name="Activity_Style" parent="@android:Theme.Holo.Light.NoActionBar"> <item name="android:background">@drawable/*</item> </style>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有