验证中...
Your message has been delivered
You aleady login OSC, would you like login with OSC
Languages: Java
Categories: Android 组件
Latest update 2017-09-13 23:34
Snippet 1 Snippet 2 Snippet 3 Snippet 4
自适应MakeXml.java
Raw Copy
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
//�ģ��������ҵĴ����������ߣ�
//ԭ�����ӣ�http://www.jianshu.com/p/ad563d169871
//����Ȩ���������У�ת������ϵ���߻����Ȩ������ע���������ߡ���
public class MakeXml {
private final static String rootPath = "E:\\layoutroot\\values-{0}x{1}\\";
// private final static float dw = 320f;
// private final static float dh = 480f;
private final static float dw = 1024f;
private final static float dh = 768f;
private final static int width_parts = 1024;
private final static int height_parts = 768;
private final static String WTemplate = "<dimen name=\"x{0}\">{1}px</dimen>\n";
private final static String HTemplate = "<dimen name=\"y{0}\">{1}px</dimen>\n";
public static void main(String argsSt[]) {
// makeString(320, 480);
// makeString(480, 800);
// makeString(480, 854);
// makeString(540, 960);
// makeString(600, 1024);
// makeString(720, 1184);
// makeString(720, 1196);
// makeString(720, 1280);
// makeString(768, 1024);
// makeString(800, 1280);
// makeString(1080, 1812);
// makeString(1080, 1920);
// makeString(1440, 2560);
// makeString(1536, 2048);
// makeString(1440, 2048);
// makeString(1600, 2560);
// makeString(600, 982);
// makeString(800,1216);
// makeString(736, 1280);
makeString(2560, 1440);
makeString(1920, 1200);
makeString(2048, 1440);
makeString(1824, 1200);
makeString(2048, 1536);
makeString(2560, 1600);
makeString(1280, 800);
makeString(1280, 752);
makeString(1024, 768);
System.out.println("制作完成");
}
public static void makeString(int w, int h) {
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
sb.append("<resources>");
float cellw = w / dw;
for (int i = 1; i < width_parts; i++) {
sb.append(WTemplate.replace("{0}", i + "").replace("{1}",
change(cellw * i) + ""));
}
sb.append(WTemplate.replace("{0}", width_parts+"").replace("{1}", w + ""));
sb.append("</resources>");
StringBuffer sb2 = new StringBuffer();
sb2.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
sb2.append("<resources>");
float cellh = h / dh;
for (int i = 1; i < height_parts; i++) {
sb2.append(HTemplate.replace("{0}", i + "").replace("{1}",
change(cellh * i) + ""));
}
sb2.append(HTemplate.replace("{0}", height_parts+"").replace("{1}", h + ""));
sb2.append("</resources>");
String path = rootPath.replace("{0}", w + "").replace("{1}", h + "");
File rootFile = new File(path);
if (!rootFile.exists()) {
rootFile.mkdirs();
}
File layxFile = new File(path + "lay_x.xml");
File layyFile = new File(path + "lay_y.xml");
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
pw.print(sb.toString());
pw.close();
pw = new PrintWriter(new FileOutputStream(layyFile));
pw.print(sb2.toString());
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static float change(float a) {
int temp = (int) (a * 100);
return temp / 100f;
}
}
String 转枚举
Raw Copy
getEnum(AssetType.class,s);
public static <T> T getEnum(Class<T> clazz, String name) {
T[] enumConstants = clazz.getEnumConstants();
for (T _enum : enumConstants) {
if (_enum.toString().toLowerCase().equals(name.toLowerCase())) {
return _enum;
}
}
return null;
}
Gson
Raw Copy
GsonPack.getInstance().fromJson(arrangmentListString, new TypeToken<ArrayList<Person>>() {}.getType());
AutoFitTextureView.java
Raw Copy
/*
* Copyright 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.camera2;
import android.content.Context;
import android.util.AttributeSet;
import android.view.TextureView;
/**
* A {@link TextureView} that can be adjusted to a specified aspect ratio.
*/
public class AutoFitTextureView extends TextureView {
private int mRatioWidth = 0;
private int mRatioHeight = 0;
public AutoFitTextureView(Context context) {
this(context, null);
}
public AutoFitTextureView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
* calculated from the parameters. Note that the actual sizes of parameters don't matter, that
* is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
*
* @param width Relative horizontal size
* @param height Relative vertical size
*/
public void setAspectRatio(int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Size cannot be negative.");
}
mRatioWidth = width;
mRatioHeight = height;
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width < height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}
}

Comment list( 0 )

You need to Sign in for post a comment