转换浸在Android中,以PXAndroid、PX

2023-09-12 11:14:51 作者:温酒叙余生

我写的方法来获得浸像素,但它不工作。它给我的运行时错误。

其实我跑这个方法在不同的类,在我的Activity类初始化

 董事会板=新的董事会(本);
board.execute(URL);
 

这code异步运行。请帮我。

 公众持股量的getPixels(INT DP){

        //资源R = boardContext.getResources();
        //浮动PX =(INT)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,干粉吸入器,r.getDisplayMetrics());

         最终浮动规模= this.boardContext.getResources()getDisplayMetrics()密度。
            INT PX =(INT)(DP *规模+ 0.5F);



        返回像素;

    }
 

解决方案

试试这个:

 公共静态浮动dipToPixels(上下文的背景下,浮动dipValue){
    DisplayMetrics指标= context.getResources()getDisplayMetrics()。
    返回TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dipValue,指标);
}
 
Android中dp和px之间如何进行转换

I had written method to get the pixels from dip but it is not working. It give me runtime error.

Actually I was running this method in separate class and initialized in my Activity class

Board board = new Board(this);
board.execute(URL);

This code run asynchronously. Please help me.

 public float getpixels(int dp){

        //Resources r = boardContext.getResources();
        //float px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpis, r.getDisplayMetrics());

         final float scale = this.boardContext.getResources().getDisplayMetrics().density;
            int px = (int) (dp * scale + 0.5f);



        return px;

    }

解决方案

Try this:

public static float dipToPixels(Context context, float dipValue) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics);
}