存档

2015年10月 的存档

android安卓判断JSONArray里面的get(i)的数据类型

2015年10月27日 没有评论

android安卓判断JSONArray里面的get(i)的数据类型:

假设test是从别处读取到的字符串,先将他转换为JSONArray,然后再循环判断

String test = “[1,2,3]”;
try {
JSONArray testA = new JSONArray(test);
for(int i = 0; i < testA.length(); i++) {
Object obj = testA.get(i);
if(obj instanceof String) {
System.out.println(“obj is String”);
} else if(obj instanceof Integer) {
System.out.println(“obj is Integer”);
}
}
System.out.println(testA.length());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

分类: android 标签: , , ,

android设置圆形头像【2种方案】

2015年10月24日 没有评论

方案1,网上应该很多人用,使用:开源类:CircleImageView

package layout.view;

import com.shiwufenzhong.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CircleImageView extends ImageView {

private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;
private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
private static final int COLORDRAWABLE_DIMENSION = 2;
private static final int DEFAULT_BORDER_WIDTH = 0;
private static final int DEFAULT_BORDER_COLOR = Color.BLACK;
private static final boolean DEFAULT_BORDER_OVERLAY = false;
private final RectF mDrawableRect = new RectF();
private final RectF mBorderRect = new RectF();
private final Matrix mShaderMatrix = new Matrix();
private final Paint mBitmapPaint = new Paint();
private final Paint mBorderPaint = new Paint();
private int mBorderColor = DEFAULT_BORDER_COLOR;
private int mBorderWidth = DEFAULT_BORDER_WIDTH;
private Bitmap mBitmap;
private BitmapShader mBitmapShader;
private int mBitmapWidth;
private int mBitmapHeight;
private float mDrawableRadius;
private float mBorderRadius;
private ColorFilter mColorFilter;
private boolean mReady;
private boolean mSetupPending;
private boolean mBorderOverlay;

public CircleImageView(Context context) {
super(context);
init();
}

public CircleImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0);
mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH);
mBorderColor = a.getColor(R.styleable.CircleImageView_border_color, DEFAULT_BORDER_COLOR);
mBorderOverlay = a.getBoolean(R.styleable.CircleImageView_border_overlay, DEFAULT_BORDER_OVERLAY);
a.recycle();
init();
}

private void init() {
super.setScaleType(SCALE_TYPE);
mReady = true;
if (mSetupPending) {
setup();
mSetupPending = false;
}
}

@Override
public ScaleType getScaleType() {
return SCALE_TYPE;
}

@Override
public void setScaleType(ScaleType scaleType) {
if (scaleType != SCALE_TYPE) {
throw new IllegalArgumentException(String.format(“ScaleType %s not supported.”, scaleType));
}
}

@Override
public void setAdjustViewBounds(boolean adjustViewBounds) {
if (adjustViewBounds) {
throw new IllegalArgumentException(“adjustViewBounds not supported.”);
}
}

@Override
protected void onDraw(Canvas canvas) {
if (getDrawable() == null) {
return;
}
canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint);
if (mBorderWidth != 0) {
canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint);
}
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
setup();
}

public int getBorderColor() {
return mBorderColor;
}

public void setBorderColor(int borderColor) {
if (borderColor == mBorderColor) {
return;
}
mBorderColor = borderColor;
mBorderPaint.setColor(mBorderColor);
invalidate();
}

@SuppressWarnings(“deprecation”)
public void setBorderColorResource(@ColorRes int borderColorRes) {
setBorderColor(getContext().getResources().getColor(borderColorRes));
}

public int getBorderWidth() {
return mBorderWidth;
}

public void setBorderWidth(int borderWidth) {
if (borderWidth == mBorderWidth) {
return;
}
mBorderWidth = borderWidth;
setup();
}

public boolean isBorderOverlay() {
return mBorderOverlay;
}

public void setBorderOverlay(boolean borderOverlay) {
if (borderOverlay == mBorderOverlay) {
return;
}
mBorderOverlay = borderOverlay;
setup();
}

@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
mBitmap = bm;
setup();
}

@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
mBitmap = getBitmapFromDrawable(drawable);
setup();
}

@Override
public void setImageResource(@DrawableRes int resId) {
super.setImageResource(resId);
mBitmap = getBitmapFromDrawable(getDrawable());
setup();
}

@Override
public void setImageURI(Uri uri) {
super.setImageURI(uri);
mBitmap = getBitmapFromDrawable(getDrawable());
setup();
}

@Override
public void setColorFilter(ColorFilter cf) {
if (cf == mColorFilter) {
return;
}
mColorFilter = cf;
mBitmapPaint.setColorFilter(mColorFilter);
invalidate();
}

private Bitmap getBitmapFromDrawable(Drawable drawable) {
if (drawable == null) {
return null;
}
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
try {
Bitmap bitmap;
if (drawable instanceof ColorDrawable) {
bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
BITMAP_CONFIG);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} catch (OutOfMemoryError e) {
return null;
}
}

private void setup() {
if (!mReady) {
mSetupPending = true;
return;
}
if (mBitmap == null) {
return;
}
mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mBitmapPaint.setAntiAlias(true);
mBitmapPaint.setShader(mBitmapShader);
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setAntiAlias(true);
mBorderPaint.setColor(mBorderColor);
mBorderPaint.setStrokeWidth(mBorderWidth);
mBitmapHeight = mBitmap.getHeight();
mBitmapWidth = mBitmap.getWidth();
mBorderRect.set(0, 0, getWidth(), getHeight());
mBorderRadius = Math.min((mBorderRect.height() – mBorderWidth) / 2, (mBorderRect.width() – mBorderWidth) / 2);
mDrawableRect.set(mBorderRect);
if (!mBorderOverlay) {
mDrawableRect.inset(mBorderWidth, mBorderWidth);
}
mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2);
updateShaderMatrix();
invalidate();
}

private void updateShaderMatrix() {
float scale;
float dx = 0;
float dy = 0;
mShaderMatrix.set(null);
if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {
scale = mDrawableRect.height() / (float) mBitmapHeight;
dx = (mDrawableRect.width() – mBitmapWidth * scale) * 0.5f;
} else {
scale = mDrawableRect.width() / (float) mBitmapWidth;
dy = (mDrawableRect.height() – mBitmapHeight * scale) * 0.5f;
}
mShaderMatrix.setScale(scale, scale);
mShaderMatrix.postTranslate((int) (dx + 0.5f) + mDrawableRect.left, (int) (dy + 0.5f) + mDrawableRect.top);
mBitmapShader.setLocalMatrix(mShaderMatrix);
}

}

 

此种方案,如果结合universal-image-loader的时候,有人说:

在列表加载的时候,这个图片是不能缓存的,每次都是用重新切图(注意,是切图,就是将缓存重新切,并不是不能缓存universal-image-loader下载的图片),重新切图就比较消耗资源了,如果是列表的话。

 

——————————————-

 

方案2:如果考虑到第一个方案的列表重新切图问题,那么你可以使用:

1:CircleBitmapDisplayer.java

2:CircleDrawable.java

源码分别是:

package com.view;

import android.graphics.Bitmap;

import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;

/**
* Created With Android Studio
* User @47
* Date 2014-07-27
* Time 20:55
* 显示原型图片的ImageLoader使用的显示器
*
*/
public class CircleBitmapDisplayer implements BitmapDisplayer {

protected  final int margin ;

public CircleBitmapDisplayer() {
this(0);
}

public CircleBitmapDisplayer(int margin) {
this.margin = margin;
}

@Override
public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
if (!(imageAware instanceof ImageViewAware)) {
throw new IllegalArgumentException(“ImageAware should wrap ImageView. ImageViewAware is expected.”);
}

imageAware.setImageDrawable(new CircleDrawable(bitmap, margin));
}

}

 

 

 

package com.view;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

/**
* Created With Android Studio
* User @47
* Date 2014-07-28
* Time 0:32
*/
public  class CircleDrawable extends Drawable {
public static final String TAG = “CircleDrawable”;

protected final Paint paint;

protected final int margin;
protected final BitmapShader bitmapShader;
protected float radius;
protected Bitmap oBitmap;//原图
public CircleDrawable(Bitmap bitmap){
this(bitmap,0);
}

public CircleDrawable(Bitmap bitmap, int margin) {
this.margin = margin;
this.oBitmap = bitmap;
bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(bitmapShader);
}

@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
computeBitmapShaderSize();
computeRadius();

}

@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();//画一个圆圈
canvas.drawCircle(bounds.width() / 2F,bounds.height() / 2F,radius,paint);
}

@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}

@Override
public void setAlpha(int alpha) {
paint.setAlpha(alpha);
}

@Override
public void setColorFilter(ColorFilter cf) {
paint.setColorFilter(cf);
}

/**
* 计算Bitmap shader 大小
*/
public void computeBitmapShaderSize(){
Rect bounds = getBounds();
if(bounds == null) return;
//选择缩放比较多的缩放,这样图片就不会有图片拉伸失衡
Matrix matrix = new Matrix();
float scaleX = bounds.width() / (float)oBitmap.getWidth();
float scaleY = bounds.height() / (float)oBitmap.getHeight();
float scale = scaleX > scaleY ? scaleX : scaleY;
matrix.postScale(scale,scale);
bitmapShader.setLocalMatrix(matrix);
}

/**
* 计算半径的大小
*/
public void computeRadius(){
Rect bounds = getBounds();
radius = bounds.width() < bounds.height() ?
bounds.width() /2F – margin:
bounds.height() / 2F – margin;
}
}

 

 

 

使用方法:

private DisplayImageOptions options;
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.tab_mine_avatar_default)
.showImageForEmptyUri(R.drawable.tab_mine_avatar_default)
.showImageOnFail(R.drawable.tab_mine_avatar_default)
.cacheInMemory(false).cacheOnDisk(false).considerExifParams(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.displayer(new CircleBitmapDisplayer())//圆形
.bitmapConfig(Bitmap.Config.RGB_565)
.build();

分类: android 标签: , ,

android安卓为什么设置dialog透明度无效?

2015年10月23日 没有评论

请问android安卓为什么设置dialog透明度无效?

呵呵,这个问题你可能很难找到原因,当然如果你出了这样的问题话。

解决答案就是:setContentView务必放在setAttributes之前,否则会导致设置透明度无效

如图:

android设置dialog透明度无效

分类: android 标签: , ,

android安卓设置dialog全屏的真正有效方法

2015年10月23日 没有评论

//在style里面配置了android:windowFullscreen=true无效
//往上相传在构造函数是super下方加setOwnerActivity((Activity)context);也无效
//用下面的函数,生效
@SuppressWarnings(“deprecation”)
public void setFullScrllWidth() {
WindowManager windowManager = (WindowManager) this.getWindow().getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lps = this.getWindow().getAttributes();
lps.width = (int) (display.getWidth());
this.getWindow().setAttributes(lps);
}

分类: android 标签: ,

android安卓从assets中读取文本和图片资源

2015年10月22日 没有评论

/** 从assets 文件夹中读取文本数据 */
public static String getTextFromAssets(final Context context, String fileName) {
String result = “”;
try {
InputStream in = context.getResources().getAssets().open(fileName);
// 获取文件的字节数
int lenght = in.available();
// 创建byte数组
byte[] buffer = new byte[lenght];
// 将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer, “UTF-8”);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

/** 从assets 文件夹中读取图片 */
public static Drawable loadImageFromAsserts(final Context ctx, String fileName) {
try {
InputStream is = ctx.getResources().getAssets().open(fileName);
return Drawable.createFromStream(is, null);
} catch (IOException e) {
if (e != null) {
e.printStackTrace();
}
} catch (OutOfMemoryError e) {
if (e != null) {
e.printStackTrace();
}
} catch (Exception e) {
if (e != null) {
e.printStackTrace();
}
}
return null;
}

分类: android 标签: , ,
css.php