Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义Android用户界面和多屏幕支持_Java_Android_User Interface - Fatal编程技术网

Java 自定义Android用户界面和多屏幕支持

Java 自定义Android用户界面和多屏幕支持,java,android,user-interface,Java,Android,User Interface,我知道以前有人问过这样的问题,但我还没有见过这样的问题。我正在使用android画布创建自定义android UI。我已经为它创建了所有必要的绘图,并将它们放在适当的文件夹中,现在我正在使用以下代码绘制UI: public class MainMenuView extends View { private Bitmap pasteImage; private Bitmap adventureLetters; private Bitmap settingsLetters; private Bit

我知道以前有人问过这样的问题,但我还没有见过这样的问题。我正在使用android画布创建自定义android UI。我已经为它创建了所有必要的绘图,并将它们放在适当的文件夹中,现在我正在使用以下代码绘制UI:

public class MainMenuView extends View {

private Bitmap pasteImage;
private Bitmap adventureLetters;
private Bitmap settingsLetters;
private Bitmap quitLetters;
private Bitmap backgroundImage;
private DisplayMetrics metrics;

public MainMenuView(Context context){
    super(context);
    pasteImage = decodeFile(R.drawable.paste, 200, true);
    Log.w("Visina imagea", String.valueOf(pasteImage.getHeight()));
    pasteImage.recycle();
}

public MainMenuView(Context context, AttributeSet attrSet){
    super(context, attrSet);
    metrics = context.getResources().getDisplayMetrics();
    pasteImage = decodeFile(R.drawable.paste, 330, true);
    adventureLetters = decodeFile(R.drawable.adventureletters, 100, true);
    settingsLetters = decodeFile(R.drawable.settingsletters, 100, true);
    quitLetters = decodeFile(R.drawable.quitletters, 50, true);
    Log.w("Je li image null? ", pasteImage == null ? "da" : "ne");
    backgroundImage = decodeFileScaled(R.drawable.toothpastebackground);
    backgroundImage = Bitmap.createScaledBitmap(backgroundImage, metrics.widthPixels - 20, metrics.heightPixels, true);
    //adventureLetters = BitmapFactory.decodeResource(getResources(), R.drawable.adventureletters);
    //settingsLetters = BitmapFactory.decodeResource(getResources(), R.drawable.settingsletters);
    //quitLetters = BitmapFactory.decodeResource(getResources(), R.drawable.quitletters);
}

@Override
protected void onMeasure(int widthMeasure, int heightMeasure){
    setMeasuredDimension(calculateMeasure(widthMeasure), calculateMeasure(heightMeasure));
    Log.w(String.valueOf(calculateMeasure(widthMeasure)), "Izmjereno");
}

private static final int DEFAULT_SIZE = 70;
private int calculateMeasure(int measure) {

    int result = (int)(DEFAULT_SIZE * getResources().getDisplayMetrics().density);
    int specMode = MeasureSpec.getMode(measure);
    int specSize = MeasureSpec.getSize(measure);

    if(specMode == MeasureSpec.EXACTLY) result = specSize;
    else if(specMode == MeasureSpec.AT_MOST){
        result = Math.min(result, specSize);
    }

    return result;
}

@Override
protected void onDraw(Canvas canvas){
    super.onDraw(canvas);
    if(canvas == null) Log.w("Canvas je null", "Da");
    else Log.w("Canvas nije null", "ne");
    Log.w("Usao u onDraw", "u onDraw sam sad");
    canvas.save();
    if(pasteImage == null) Log.w("Slika je null", "null slika");
    canvas.drawBitmap(backgroundImage, 0, 0, null);
    Log.w("Dimenzije slike sirina x visina bg", backgroundImage.getWidth() + " " + backgroundImage.getHeight());
    canvas.drawBitmap(pasteImage, 110, 420, null);
    canvas.drawBitmap(adventureLetters, 50,  60, null);
    canvas.drawBitmap(settingsLetters, 80,  60, null);
    canvas.drawBitmap(quitLetters, 100,  60, null);
    pasteImage.recycle();
    adventureLetters.recycle();
    settingsLetters.recycle();
    quitLetters.recycle();
    canvas.restore();
}

private Bitmap decodeFile(int id, int size, boolean resize){

        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), id, o);

        //The new size we want to scale to
        final int REQUIRED_SIZE = size;

        //Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while(o.outWidth/scale/2 >= REQUIRED_SIZE && o.outHeight/scale/2 >= REQUIRED_SIZE)
            scale*=2;

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeResource(getResources(), id, o2);

}

private Bitmap decodeFileScaled(int id){

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), id, options);
    Log.w("Sirina x visina displaya", metrics.widthPixels + "x" +   metrics.heightPixels);
    options.inSampleSize = calculateScale(options, metrics.widthPixels, metrics.heightPixels);

    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(getResources(), id, options);
}

private int calculateScale(BitmapFactory.Options options, int reqWidth, int reqHeight){

    final int rawHeight = options.outHeight;
    final int rawWidth = options.outWidth;
    int inSampleSize = 1;

    if (rawHeight > reqHeight || rawWidth > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) rawHeight / (float) reqHeight);
        final int widthRatio = Math.round((float) rawWidth / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}
}
public类主菜单视图扩展视图{
私有位图图像;
私人信件;
私人位图设置;
私人信件;
私有位图背景图像;
私有显示度量;
公共主菜单视图(上下文){
超级(上下文);
pasteImage=decodeFile(R.drawable.paste,200,true);
Log.w(“Visina imagea”,String.valueOf(pasteImage.getHeight());
pasteImage.recycle();
}
公共主菜单视图(上下文上下文、属性集属性集){
super(上下文,属性集);
metrics=context.getResources().getDisplayMetrics();
pasteImage=decodeFile(R.drawable.paste,330,true);
adventureLetters=解码文件(R.drawable.adventureLetters,100,true);
settingsLetters=decodeFile(R.drawable.settingsLetters,100,true);
quitLetters=解码文件(R.drawable.quitLetters,50,true);
Log.w(“Je li image null?”,pasteImage==null?”da:“ne”);
backgroundImage=解码文件缩放(R.drawable.牙膏背景);
backgroundImage=Bitmap.createScaledBitmap(backgroundImage,metrics.widthPixels-20,metrics.heightPixels,true);
//adventureLetters=BitmapFactory.decodeResource(getResources(),R.drawable.adventureLetters);
//settingsLetters=BitmapFactory.decodeResource(getResources(),R.drawable.settingsLetters);
//quitLetters=BitmapFactory.decodeResource(getResources(),R.drawable.quitLetters);
}
@凌驾
测量时受保护的空隙(整数宽度测量,整数高度测量){
设置测量尺寸(计算测量(宽度测量)、计算测量(高度测量));
Log.w(String.valueOf(calculateMeasure(widthmasure)),“Izmjereno”);
}
私有静态最终整数默认值_大小=70;
专用整数计算度量(整数度量){
int result=(int)(默认值_SIZE*getResources().getDisplayMetrics().density);
int specMode=MeasureSpec.getMode(度量);
int specSize=MeasureSpec.getSize(度量);
如果(specMode==MeasureSpec.justice)结果=specSize;
else if(specMode==MeasureSpec.AT_){
结果=数学最小值(结果、规格尺寸);
}
返回结果;
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
如果(canvas==null)Log.w(“canvas-je-null”,“Da”);
else Log.w(“Canvas nije null”,“ne”);
Log.w(“Usao u onDraw”、“u onDraw sam sad”);
canvas.save();
if(pasteImage==null)Log.w(“Slika je null”,“null Slika”);
drawBitmap(背景图像,0,0,null);
Log.w(“Dimenzije slike sirina x visina bg”,backgroundImage.getWidth()+“”+backgroundImage.getHeight());
drawBitmap(粘贴图像,110,420,空);
canvas.drawBitmap(adventureLetters,50,60,null);
drawBitmap(setingsletters,80,60,null);
canvas.drawBitmap(quitLetters,100,60,null);
pasteImage.recycle();
adventureLetters.recycle();
setingsletters.recycle();
quitLetters.recycle();
canvas.restore();
}
私有位图解码文件(整数id、整数大小、布尔值大小){
//解码图像大小
BitmapFactory.Options o=新的BitmapFactory.Options();
o、 inJustDecodeBounds=true;
解码资源(getResources(),id,o);
//我们要扩展到的新尺寸
所需最终整型尺寸=尺寸;
//找到正确的刻度值。它应该是2的幂。
int标度=1;
而(o.outWidth/scale/2>=所需尺寸和&o.outHeight/scale/2>=所需尺寸)
比例*=2;
//用inSampleSize解码
BitmapFactory.Options o2=新的BitmapFactory.Options();
o2.inSampleSize=刻度;
返回BitmapFactory.decodeResource(getResources(),id,o2);
}
私有位图解码文件缩放(int id){
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码资源(getResources(),id,选项);
Log.w(“Sirina x visina displaya”,metrics.widthPixels+“x”+metrics.heightPixels);
options.inSampleSize=calculateScale(选项,metrics.widthPixels,metrics.heightPixels);
options.inJustDecodeBounds=false;
返回BitmapFactory.decodeResource(getResources(),id,options);
}
专用int calculateScale(BitmapFactory.Options选项、int reqWidth、int reqHeight){
最终int rawHeight=选项.OUTHIGHT;
最终int-rawWidth=options.outWidth;
int inSampleSize=1;
if(原始高度>要求高度| |原始宽度>要求宽度){
//计算高度和宽度与所需高度和宽度的比率
最终内部高度比=数学圆((浮动)原始高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮点)rawWidth/(浮点)reqWidth);
//选择最小比率作为采样值,这将保证
//最终图像的两个尺寸均大于或等于
//要求的高度和宽度。
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}
}

您可以立即看到,我正在使用一些硬编码值在屏幕上定位。你能给我一些关于如何编码它的建议,使它在每个屏幕上都像它应该的那样出现(特别是中高维)。因为它应该意味着没有模糊撕裂空空间等。如果你需要更多的信息,请在评论中告诉我,非常感谢

您应该始终使用与屏幕相关的位置。例如:

float left = getScreenSize().x * 0.10f;
float top = getScreenSize().y * 0.10f;

canvas.drawBitmap(pasteImage, left, top, null);

private float getScreenSize() {
     WindowManager windowManager = (WindowManager) getContext.getSystemService(Context.WINDOW_SERVICE);
     Display display = windowManager.getDefaultDisplay();
     Display display = getWindowManager().getDefaultDisplay();
     Point size = new Point();
     display.getSize(size);
     return size;
}
但是如果你要用绝对值,你应该用dp,而不是px

Resources resources = getResources();

float left = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 140, resources.getDisplayMetrics());

float top = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 140, resources.getDisplayMetrics());

canvas.drawBitmap(pasteImage, left, top, null);

有没有办法避免创建这些神奇的数字(宽度*某物)等等?神奇?:)如果使用视图而不是绘制
D