分类 技术 下的文章

PHP用curl访问https

昨天用php改写here的api,在做微博登录的时候,需要curl一下微博的api获取用户信息,但是微博的api是https协议,刚开始死活都访问不了,查了半天,才整明白。解决办法如下:

  1. 下载CAcert.pem,在windows中最好下载打包成rar的文件再解压出来,然后在属性中解除限制
  2. 在php.ini中修改curl.cainfo为CAcert.pem的地址
  3. 重启php

Done

安卓6.0修复org.apache包缺失问题

最近把build tool换成23之后,开始出现各种错误,包括gradle打包apk过程中出现警告,还有app莫名崩溃,在网上查了一下,是因为安卓6.0把org.apache.http从包中除去有关系,解决办法:

  1. 找到org.apache.http.legacy.jar并复制到libs里
  2. 在gradle中添加dependencies
  3. 在proguard-rules.pro中写入:
    -dontwarn org.apache.**

-keep class org.apache.* { public <methods>; !private ; }
-keep class android.net.* { public <methods>; !private ; }
-keep class com.google.android.vending.licensing.* { public <methods>; !private ; }
可再适当的添加或删减rules

Done

ImageMovieAnimator - Image sliding Animator

This class is used to display a slide-moving image.

Usage

    ImageMovieAnimator imageAnimator = new ImageMovieAnimator(activity,(ImageView)findViewById(R.id.image),
                new int[]{R.drawable.image1,R.drawable.image2,...});
    imageAnimator.start();
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import me.lvxingshe.android.MyApplication;
import me.lvxingshe.android.R;

/**
 * Created by Luncrzs on 2015/10/18.
 */
public class ImageMovieAnimator {
    private Context mContext;
    private ImageView iv;
    private int[] images;
    private int thisImageId = 0;
    private int screenWidth = 0;
    private int screenHeight = 0;
    Bitmap bitmap;
    ValueAnimator animator;

    public ImageMovieAnimator(Context context, ImageView imageView, int[] imageResources){
        mContext = context;
        iv = imageView;
        images = imageResources;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(displayMetrics);
        screenHeight = displayMetrics.heightPixels + getNavigationBarHeight();
        Log.d("ImageAnimator","height="+displayMetrics.heightPixels+" screenHeight="+screenHeight);
        screenWidth = displayMetrics.widthPixels;
    }

    public void start(){
        bitmap = BitmapFactory.decodeResource(mContext.getResources(),images[thisImageId]);
        iv.setImageBitmap(bitmap);
        double screenRatio = (double)screenWidth/screenHeight;
        double bitmapRatio = (double)bitmap.getWidth()/bitmap.getHeight();
        final int imageHeight = screenRatio<bitmapRatio?screenHeight:(int)(((double)screenWidth/bitmap.getWidth())*bitmap.getHeight());
        final int imageWidth = screenRatio<bitmapRatio?(int)(((double)screenHeight/bitmap.getHeight())*bitmap.getWidth()):screenWidth;
        final int paddingValue = (imageWidth-screenWidth)/2;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageWidth,imageHeight);
        iv.setLayoutParams(params);
        iv.setPadding(paddingValue, 0, 0, 0);
        animator = ValueAnimator.ofFloat(1, -1);
        animator.setDuration(screenRatio < bitmapRatio ? paddingValue * 50 : 5000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();

                iv.setPadding((int) (value * paddingValue), 0, 0, 0);
                if (value > 0.9) {
                    iv.setAlpha((float) (0.3 + 7 * (1 - value)));
                } else if (value < -0.9) {
                    iv.setAlpha((float) (0.3 + 7 * (value + 1)));
                }
                iv.invalidate();
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                thisImageId = (thisImageId + 1) < images.length ? thisImageId + 1 : 0;
                start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
    }

    public int getNavigationBarHeight(){
        Resources resources = mContext.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        }
        return 0;
    }

    public void setHeight(int height){
        screenHeight = height;
    }

    public void stop(){
        if(animator!=null && animator.isRunning()){
            animator.end();
        }
    }
}

Shuffle Utility

This class is used to shuffle a list

import java.util.List;
import java.util.Random;

/**
 * Created by luncrzs on 16/1/27.
 */
public class Shuffle {

    public static List shuffle(List list){
        Random random = new Random(System.currentTimeMillis());
        for(int i=0; i<list.size(); i++){
            int a = random.nextInt(list.size());
            int b = random.nextInt(list.size());
            if(a == b){
                continue;
            }
            Object aObject = list.get(a);
            Object bObject = list.get(b);
            list.remove(a);
            list.add(a,bObject);
            list.remove(b);
            list.add(b,aObject);
        }
        return list;
    }

    public static int[][] shuffle(int[][] arr){
        Random random = new Random(System.currentTimeMillis());
        for(int i=0; i<arr.length; i++){
            int a = random.nextInt(arr.length);
            int b = random.nextInt(arr.length);
            if(a == b){
                continue;
            }
            int[] tmp = new int[arr[0].length];
            for(int j=0; j<arr[0].length; j++){
                tmp[j] = arr[a][j];
            }
            for(int j=0; j<arr[0].length; j++){
                arr[a][j] = arr[b][j];
            }
            for(int j=0; j<arr[0].length; j++){
                arr[b][j] = tmp[j];
            }
        }
        return arr;
    }
}