String.format("%1$02d", Integer.valueOf(month))
“%1$02d”がミソ。2桁に満たない場合は0が加えられ、結果的に2桁表示にしてくれる。
1だと 01 というふうに。
“%1$04d” にすれば、
1だと 0001 というふうになる。
タイマーをよく使うのですが、備忘録として。
// タイマー作動 Timer timer1 = new Timer(); Handler handler1 = new Handler(); timer1.schedule(new DownloadTimer(), 0, 2000);
タイマー、ハンドラーを宣言。その後のスケジュールで0秒から開始、その後2000ミリ秒ごと(2秒)にタイマー作動。となっています。 new DownloadTimer() を宣言してそれを実行することになっています。
実際の処理内は、
class DownloadTimer extends TimerTask {
@Override
public void run() {
handler1.post(new Runnable(){
@Override
public void run() {
}
});
}
}
こんな感じでクラスをつくり public void run() { } に処理をかきます。それが実行されます。
public class ImageManager {
private Context mContext;
private String fileFullPath;
public ImageManager(Context context) {
mContext = context;
}
/**
* 画像の保存
*
* @param bitmap
* @param albumName
*/
public void save(Bitmap bitmap, String albumName) {
if (!canUseSd()) {
Log.e("Error", "Can't use SD Card");
return;
}
saveToSd(getSdStorageDir(albumName), bitmap);
}
/**
* ストレージに画像保存
*
* @param dir
* @param bitmap
*/
private void saveToSd(File dir, Bitmap bitmap) {
String fileName = getFileName();
fileFullPath = dir.getAbsolutePath() + "/" + fileName;
Log.d("保存パス","fileFullPath= " + fileFullPath);
try {
// 保存処理
FileOutputStream fos = new FileOutputStream(fileFullPath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (Exception e) {
Log.e("Error", "" + e.toString());
} finally {
// アルバムに反映
addGallery(fileName);
}
}
/**
* 保存した画像をギャラリーに追加
*
* @param fileName
*/
private void addGallery(String fileName) {
try {
ContentValues values = new ContentValues();
ContentResolver contentResolver = mContext.getContentResolver();
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put("_data", fileFullPath);
contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} catch (Exception e) {
Log.e("Error", "" + e);
}
}
/**
* 画像のファイル名を日付から生成し取得
*
* @return
*/
private String getFileName() {
Date mDate = new Date();
SimpleDateFormat fileNameFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH);
String fileName = fileNameFormat.format(mDate) + ".jpg";
return fileName;
}
/**
* ストレージのストレージパス取得
*
* @param albumName
* @return
*/
private File getSdStorageDir(String albumName) {
File dir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (!dir.exists()) {
if (!dir.mkdirs()) {
Log.e("Error", "Directory not created");
}
}
return dir;
}
/**
* ストレージが読み込み可能か
*
* @return
*/
public boolean canReadSd() {
String status = Environment.getExternalStorageState();
if (!status.equals(Environment.MEDIA_MOUNTED)) {
return false;
}
File file = Environment.getExternalStorageDirectory();
if (file.canRead()) {
return true;
}
return false;
}
/**
* ストレージに書き込み可能か
*
* @return
*/
public boolean canWriteSd() {
String status = Environment.getExternalStorageState();
if (!status.equals(Environment.MEDIA_MOUNTED)) {
return false;
}
File file = Environment.getExternalStorageDirectory();
if (file.canWrite()) {
return true;
}
return false;
}
/**
* ストレージが使用可能か
*
* @return
*/
public boolean canUseSd() {
return canReadSd() && canWriteSd();
}
}
使い方は、
ImageView imageView = (ImageView) findViewById(R.id.photo_image);
Bitmap imageForScale = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
ImageManager imageManager = new ImageManager(this);
try {
String albumName = "Save image sample";
imageManager.save(imageForScale, albumName);
} catch (Error e) {
Log.e("MainActivity", "onCreate: " + e);
}
FrameLayout main = (FrameLayout) findViewById(R.id.main); // 移動アニメーション TranslateAnimation2 translateAnimation = new TranslateAnimation2(1400.0f, 0.0f, 0.0f, 0.0f, Easing.easeInOutQuart); translateAnimation.setDuration(1200); translateAnimation.setFillAfter(true); main.startAnimation(translateAnimation);
TranslateAnimation2クラスとか
Easingクラスとかいるからね
public class Easing {
public static String easeInQuad = "easeInQuad";
public static String easeOutQuad = "easeOutQuad";
public static String easeInOutQuad = "easeInOutQuad";
public static String easeInCubic = "easeInCubic";
public static String easeOutCubic = "easeOutCubic";
public static String easeInOutCubic = "easeInOutCubic";
public static String easeInQuart = "easeInQuart";
public static String easeOutQuart = "easeOutQuart";
public static String easeInOutQuart = "easeInOutQuart";
public static String easeInQuint = "easeInQuint";
public static String easeOutQuint = "easeOutQuint";
public static String easeInOutQuint = "easeInOutQuint";
public static String easeInSine = "easeInSine";
public static String easeOutSine = "easeOutSine";
public static String easeInOutSine = "easeInOutSine";
public static String easeInExpo = "easeInExpo";
public static String easeOutExpo = "easeOutExpo";
public static String easeInOutExpo = "easeInOutExpo";
public static String easeInCirc = "easeInCirc";
public static String easeOutCirc = "easeOutCirc";
public static String easeInOutCirc = "easeInOutCirc";
public static String easeInElastic = "easeInElastic";
public static String easeOutElastic = "easeOutElastic";
public static String easeInOutElastic = "easeInOutElastic";
public static String easeInBack = "easeInBack";
public static String easeOutBack = "easeOutBack";
public static String easeInOutBack = "easeInOutBack";
public static String easeInBounce = "easeInBounce";
public static String easeOutBounce = "easeOutBounce";
public static String easeInOutBounce = "easeInOutBounce";
public static float move(float t, String easing) {
if (easing.equals(easeInQuad)) {
return easeInQuad(t);
} else if (easing.equals(easeOutQuad)) {
return easeOutQuad(t);
} else if (easing.equals(easeInOutQuad)) {
return easeInOutQuad(t);
} else if (easing.equals(easeInCubic)) {
return easeInCubic(t);
} else if (easing.equals(easeOutCubic)) {
return easeOutCubic(t);
} else if (easing.equals(easeInOutCubic)) {
return easeInOutCubic(t);
} else if (easing.equals(easeInQuart)) {
return easeInQuart(t);
} else if (easing.equals(easeOutQuart)) {
return easeOutQuart(t);
} else if (easing.equals(easeInOutQuart)) {
return easeInOutQuart(t);
} else if (easing.equals(easeInQuint)) {
return easeInQuint(t);
} else if (easing.equals(easeOutQuint)) {
return easeOutQuint(t);
} else if (easing.equals(easeInOutQuint)) {
return easeInOutQuint(t);
} else if (easing.equals(easeInSine)) {
return easeInSine(t);
} else if (easing.equals(easeOutSine)) {
return easeOutSine(t);
} else if (easing.equals(easeInOutSine)) {
return easeInOutSine(t);
} else if (easing.equals(easeInExpo)) {
return easeInExpo(t);
} else if (easing.equals(easeOutExpo)) {
return easeOutExpo(t);
} else if (easing.equals(easeInOutExpo)) {
return easeInOutExpo(t);
} else if (easing.equals(easeInCirc)) {
return easeInCirc(t);
} else if (easing.equals(easeOutCirc)) {
return easeOutCirc(t);
} else if (easing.equals(easeInOutCirc)) {
return easeInOutCirc(t);
} else if (easing.equals(easeInElastic)) {
return easeInElastic(t);
} else if (easing.equals(easeOutElastic)) {
return easeOutElastic(t);
} else if (easing.equals(easeInOutElastic)) {
return easeInOutElastic(t);
} else if (easing.equals(easeInBack)) {
return easeInBack(t);
} else if (easing.equals(easeOutBack)) {
return easeOutBack(t);
} else if (easing.equals(easeInOutBack)) {
return easeInOutBack(t);
} else if (easing.equals(easeInBounce)) {
return easeInBounce(t);
} else if (easing.equals(easeOutBounce)) {
return easeOutBounce(t);
} else if (easing.equals(easeInOutBounce)) {
return easeInOutBounce(t);
}
return t;
}
public static float easeInQuad(float t) {
return easeInQuad(t, 0f, 1f, 1f);
}
public static float easeOutQuad(float t) {
return easeInQuad(t, 0f, 1f, 1f);
}
public static float easeInOutQuad(float t) {
return easeInOutQuad(t, 0f, 1f, 1f);
}
public static float easeInCubic(float t) {
return easeInCubic(t, 0f, 1f, 1f);
}
public static float easeOutCubic(float t) {
return easeOutCubic(t, 0f, 1f, 1f);
}
public static float easeInOutCubic(float t) {
return easeInOutCubic(t, 0f, 1f, 1f);
}
public static float easeInQuart(float t) {
return easeInQuart(t, 0f, 1f, 1f);
}
public static float easeOutQuart(float t) {
return easeOutQuart(t, 0f, 1f, 1f);
}
public static float easeInOutQuart(float t) {
return easeInOutQuart(t, 0f, 1f, 1f);
}
public static float easeInQuint(float t) {
return easeInQuint(t, 0f, 1f, 1f);
}
public static float easeOutQuint(float t) {
return easeOutQuint(t, 0f, 1f, 1f);
}
public static float easeInOutQuint(float t) {
return easeInOutQuint(t, 0f, 1f, 1f);
}
public static float easeInSine(float t) {
return easeInSine(t, 0f, 1f, 1f);
}
public static float easeOutSine(float t) {
return easeOutSine(t, 0f, 1f, 1f);
}
public static float easeInOutSine(float t) {
return easeInOutSine(t, 0f, 1f, 1f);
}
public static float easeInExpo(float t) {
return easeInExpo(t, 0f, 1f, 1f);
}
public static float easeOutExpo(float t) {
return easeOutExpo(t, 0f, 1f, 1f);
}
public static float easeInOutExpo(float t) {
return easeInOutExpo(t, 0f, 1f, 1f);
}
public static float easeInCirc(float t) {
return easeInCirc(t, 0f, 1f, 1f);
}
public static float easeOutCirc(float t) {
return easeOutCirc(t, 0f, 1f, 1f);
}
public static float easeInOutCirc(float t) {
return easeInOutCirc(t, 0f, 1f, 1f);
}
public static float easeInElastic(float t) {
return easeInElastic(t, 0f, 1f, 1f);
}
public static float easeOutElastic(float t) {
return easeOutElastic(t, 0f, 1f, 1f);
}
public static float easeInOutElastic(float t) {
return easeInOutElastic(t, 0f, 1f, 1f);
}
public static float easeInBack(float t) {
return easeInBack(t, 0f, 1f, 1f, 1.70158f);
}
public static float easeOutBack(float t) {
return easeOutBack(t, 0f, 1f, 1f, 1.70158f);
}
public static float easeInOutBack(float t) {
return easeInOutBack(t, 0f, 1f, 1f, 1.70158f);
}
public static float easeInBounce(float t) {
return easeInBounce(t, 0f, 1f, 1f);
}
public static float easeOutBounce(float t) {
return easeOutBounce(t, 0f, 1f, 1f);
}
public static float easeInOutBounce(float t) {
return easeInOutBounce(t, 0f, 1f, 1f);
}
/**/
public static float easeInQuad(float t, float b, float c, float d) {
return c * (t /= d) * t + b;
}
public static float easeOutQuad(float t, float b, float c, float d) {
return -c * (t /= d) * (t - 2f) + b;
}
public static float easeInOutQuad(float t, float b, float c, float d) {
if ((t /= d / 2f) < 1)
return c / 2f * t * t + b;
return -c / 2f * ((--t) * (t - 2f) - 1) + b;
}
public static float easeInCubic(float t, float b, float c, float d) {
return c * (t /= d) * t * t + b;
}
public static float easeOutCubic(float t, float b, float c, float d) {
return c * ((t = t / d - 1f) * t * t + 1f) + b;
}
public static float easeInOutCubic(float t, float b, float c, float d) {
if ((t /= d / 2f) < 1)
return c / 2f * t * t * t + b;
return c / 2f * ((t -= 2f) * t * t + 2f) + b;
}
public static float easeInQuart(float t, float b, float c, float d) {
return c * (t /= d) * t * t * t + b;
}
public static float easeOutQuart(float t, float b, float c, float d) {
return -c * ((t = t / d - 1f) * t * t * t - 1f) + b;
}
public static float easeInOutQuart(float t, float b, float c, float d) {
if ((t /= d / 2f) < 1f)
return c / 2f * t * t * t * t + b;
return -c / 2f * ((t -= 2f) * t * t * t - 2f) + b;
}
public static float easeInQuint(float t, float b, float c, float d) {
return c * (t /= d) * t * t * t * t + b;
}
public static float easeOutQuint(float t, float b, float c, float d) {
return c * ((t = t / d - 1f) * t * t * t * t + 1f) + b;
}
public static float easeInOutQuint(float t, float b, float c, float d) {
if ((t /= d / 2f) < 1f)
return c / 2f * t * t * t * t * t + b;
return c / 2f * ((t -= 2f) * t * t * t * t + 2f) + b;
}
public static float easeInSine(float t, float b, float c, float d) {
return (float) (-c * Math.cos(t / d * (Math.PI / 2f)) + c + b);
}
public static float easeOutSine(float t, float b, float c, float d) {
return (float) (c * Math.sin(t / d * (Math.PI / 2f)) + b);
}
public static float easeInOutSine(float t, float b, float c, float d) {
return (float) (-c / 2f * (Math.cos(Math.PI * t / d) - 1) + b);
}
public static float easeInExpo(float t, float b, float c, float d) {
return (float) ((t == 0) ? b : c * Math.pow(2, 10f * (t / d - 1f)) + b);
}
public static float easeOutExpo(float t, float b, float c, float d) {
return (float) ((t == d) ? b + c : c * (-Math.pow(2, -10f * t / d) + 1f) + b);
}
public static float easeInOutExpo(float t, float b, float c, float d) {
if (t == 0)
return b;
if (t == d)
return b + c;
if ((t /= d / 2f) < 1f)
return (float) (c / 2f * Math.pow(2, 10f * (t - 1f)) + b);
return (float) (c / 2f * (-Math.pow(2, -10f * --t) + 2f) + b);
}
public static float easeInCirc(float t, float b, float c, float d) {
return (float) (-c * (Math.sqrt(1f - (t /= d) * t) - 1f) + b);
}
public static float easeOutCirc(float t, float b, float c, float d) {
return (float) (c * Math.sqrt(1 - (t = t / d - 1) * t) + b);
}
public static float easeInOutCirc(float t, float b, float c, float d) {
if ((t /= d / 2) < 1)
return (float) (-c / 2 * (Math.sqrt(1 - t * t) - 1) + b);
return (float) (c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b);
}
public static float easeInElastic(float t, float b, float c, float d) {
float s = 1.70158f;
float p = 0;
float a = c;
if (t == 0)
return b;
if ((t /= d) == 1)
return b + c;
p = d * 0.3f;
if (a < Math.abs(c)) {
a = c;
s = p / 4f;
} else
s = (float) (p / (2f * Math.PI) * Math.asin(c / a));
return (float) (-(a * Math.pow(2, 10f * (t -= 1f)) * Math.sin((t * d - s) * (2f * Math.PI) / p)) + b);
}
public static float easeOutElastic(float t, float b, float c, float d) {
float s = 1.70158f;
float p = 0;
float a = c;
if (t == 0)
return b;
if ((t /= d) == 1)
return b + c;
p = d * 0.3f;
if (a < Math.abs(c)) {
a = c;
s = p / 4f;
} else
s = (float) (p / (2f * Math.PI) * Math.asin(c / a));
return (float) (a * Math.pow(2, -10f * t) * Math.sin((t * d - s) * (2f * Math.PI) / p) + c + b);
}
public static float easeInOutElastic(float t, float b, float c, float d) {
float s = 1.70158f;
float p = 0;
float a = c;
if (t == 0)
return b;
if ((t /= d / 2) == 2)
return b + c;
p = d * (0.3f * 1.5f);
if (a < Math.abs(c)) {
a = c;
s = p / 4f;
} else
s = (float) (p / (2f * Math.PI) * Math.asin(c / a));
if (t < 1)
return (float) (-0.5f * (a * Math.pow(2, 10f * (t -= 1f)) * Math.sin((t * d - s) * (2f * Math.PI) / p)) + b);
return (float) (a * Math.pow(2, -10f * (t -= 1f)) * Math.sin((t * d - s) * (2f * Math.PI) / p) * 0.5f + c + b);
}
public static float easeInBack(float t, float b, float c, float d, float s) {
return c * (t /= d) * t * ((s + 1f) * t - s) + b;
}
public static float easeOutBack(float t, float b, float c, float d, float s) {
return c * ((t = t / d - 1f) * t * ((s + 1f) * t + s) + 1f) + b;
}
public static float easeInOutBack(float t, float b, float c, float d, float s) {
if ((t /= d / 2f) < 1)
return c / 2f * (t * t * (((s *= (1.525f)) + 1f) * t - s)) + b;
return c / 2f * ((t -= 2f) * t * (((s *= (1.525f)) + 1f) * t + s) + 2f) + b;
}
public static float easeInBounce(float t, float b, float c, float d) {
return c - easeOutBounce(d - t, 0, c, d) + b;
}
public static float easeOutBounce(float t, float b, float c, float d) {
if ((t /= d) < (1f / 2.75f)) {
return c * (7.5625f * t * t) + b;
} else if (t < (2f / 2.75f)) {
return c * (7.5625f * (t -= (1.5f / 2.75f)) * t + 0.75f) + b;
} else if (t < (2.5f / 2.75f)) {
return c * (7.5625f * (t -= (2.25f / 2.75f)) * t + 0.9375f) + b;
} else {
return c * (7.5625f * (t -= (2.625f / 2.75f)) * t + 0.984375f) + b;
}
}
public static float easeInOutBounce(float t, float b, float c, float d) {
if (t < d / 2f)
return easeInBounce(t * 2f, 0, c, d) * 0.5f + b;
return easeOutBounce(t * 2f - d, 0, c, d) * 0.5f + c * 0.5f + b;
}
}
そんでもって
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
public class TranslateAnimation2 extends Animation {
private int mFromXType = ABSOLUTE;
private int mToXType = ABSOLUTE;
private int mFromYType = ABSOLUTE;
private int mToYType = ABSOLUTE;
private float mFromXValue = 0.0f;
private float mToXValue = 0.0f;
private float mFromYValue = 0.0f;
private float mToYValue = 0.0f;
private float mFromXDelta;
private float mToXDelta;
private float mFromYDelta;
private float mToYDelta;
private String easing;
/**
* Constructor to use when building a TranslateAnimation from code
*
* @param fromXDelta
* Change in X coordinate to apply at the start of the animation
* @param toXDelta
* Change in X coordinate to apply at the end of the animation
* @param fromYDelta
* Change in Y coordinate to apply at the start of the animation
* @param toYDelta
* Change in Y coordinate to apply at the end of the animation
*/
public TranslateAnimation2(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta,String _easing) {
mFromXValue = fromXDelta;
mToXValue = toXDelta;
mFromYValue = fromYDelta;
mToYValue = toYDelta;
mFromXType = ABSOLUTE;
mToXType = ABSOLUTE;
mFromYType = ABSOLUTE;
mToYType = ABSOLUTE;
easing = _easing;
setInterpolator(new LinearInterpolator());
}
/**
* Constructor to use when building a TranslateAnimation from code
*
* @param fromXType
* Specifies how fromXValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param fromXValue
* Change in X coordinate to apply at the start of the animation.
* This value can either be an absolute number if fromXType is
* ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
* @param toXType
* Specifies how toXValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param toXValue
* Change in X coordinate to apply at the end of the animation.
* This value can either be an absolute number if toXType is
* ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
* @param fromYType
* Specifies how fromYValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param fromYValue
* Change in Y coordinate to apply at the start of the animation.
* This value can either be an absolute number if fromYType is
* ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
* @param toYType
* Specifies how toYValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param toYValue
* Change in Y coordinate to apply at the end of the animation.
* This value can either be an absolute number if toYType is
* ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
public TranslateAnimation2(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue,String _easing) {
mFromXValue = fromXValue;
mToXValue = toXValue;
mFromYValue = fromYValue;
mToYValue = toYValue;
mFromXType = fromXType;
mToXType = toXType;
mFromYType = fromYType;
mToYType = toYType;
setInterpolator(new LinearInterpolator());
easing = _easing;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float dx = mFromXDelta;
float dy = mFromYDelta;
if (mFromXDelta != mToXDelta) {
dx = mFromXDelta + ((mToXDelta - mFromXDelta) * Easing.move(interpolatedTime,easing));
}
if (mFromYDelta != mToYDelta) {
dy = mFromYDelta + ((mToYDelta - mFromYDelta) * Easing.move(interpolatedTime,easing));
}
t.getMatrix().setTranslate(dx, dy);
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mFromXDelta = resolveSize(mFromXType, mFromXValue, width, parentWidth);
mToXDelta = resolveSize(mToXType, mToXValue, width, parentWidth);
mFromYDelta = resolveSize(mFromYType, mFromYValue, height, parentHeight);
mToYDelta = resolveSize(mToYType, mToYValue, height, parentHeight);
}
}
つかうときはこげな感じ
FrameLayout main = (FrameLayout) findViewById(R.id.main); //てけとーなレイアウトとかを TranslateAnimation2 translateAnimation = new TranslateAnimation2(-1400.0f, 0.0f, 0.0f, 0.0f, Easing.easeOutBounce); translateAnimation.setDuration(1000); translateAnimation.setFillAfter(true); main.startAnimation(translateAnimation);
こんな風にすると、びっくりただのアニメーションにバウンスついたりする。これはJavaScriptでもよくある手法だよぬ!
TranslateAnimation2(-1400.0f, 0.0f, 0.0f, 0.0f, Easing.easeOutBounce);
ポイントは上の一行ですね。
クラスファイルを2つ使用するだけでいけるので、ちょびっとアニメーションしたいときなんか簡単でいいですよね。
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("登録しました");
builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 遷移
Intent intent = new Intent(getApplication(), testActivity.class);
startActivity(intent);
}
});
// 表示
builder.create().show();
これでウインドメッセージが出てきます。
“OK” を押した場合は、
builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 遷移
Intent intent = new Intent(getApplication(), G_2_0_4_SeijinshikiActivity.class);
startActivity(intent);
}
});
この中にかくといいよ!
でもダイアログ以外をおしたときは、どう判定すればいいのか、、、、
ImageView photo_thum = (ImageView) view2.findViewById(R.id.photo_thum); photo_thum.setMargin();
みたいにできると思うやん?できんから。
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)photo_thum.getLayoutParams(); lp.rightMargin = 10; photo_thum.setLayoutParams(lp);
一度、MarginLayoutParams に通すのね。これを利用すればなんでもオッケーオッケー。
// 端末の横幅を元に、3分割した横幅を計算
public int getTanmatsuWidth(int width) {
width -= 20;
int w = (int)(width/3);
Log.d(TAG, "w= " + w);
return w;
}
// 端末の横幅を元に、3分割した横幅を計算
public int getTamatsuHeight(int width) {
width -= 20;
int width2 = (width/3);
int h = (int)(width2/1.618);
Log.d(TAG, "h= " + h);
return h;
}
端末に画像を3つ並べて、間に10dpの隙間がはいることを想定しています。
なんちゃないよ。
// 端末の横DPを取得 WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); Display dp = wm.getDefaultDisplay();
これで取れます。
tanmatsuWidth = dp.getWidth();
こうすればいいよぬ!
ただ、今は非奨励なんで
Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y;
こっちでとりましょう。
http://d.hatena.ne.jp/tomorrowkey/20110809/1312865800
まずはここを見る、、、 認証とかめんどくさい。一般公開しているスプレッドシートの場合はどーなるというのか?!
ほんっと、みんな複雑に説明するの好きよね。頭いかれてる。
すこーしずつやってみていいやんか。
というわけで少しづつJSONを作ってみるから。ああ、あとさ。「作ってみる」を「作成」とかそういう風に難しく言うのも好きじゃない。頭いかれてる。 てめーだよ、てめー。
JSONObject jsonObject = new JSONObject();
String name = "いやんばかん";
jsonObject.put("name", name);
return jsonObject;
まず、こうする。自分の予想的には立派なJSONが出てくるだろうと思ってる、しかし
jsonObject.put(“name”, name);
ここでエラーでます。1時間苦しむ。
答え↓
JSONObject jsonObject = new JSONObject();
String name = "いやんばかん";
try {
jsonObject.put("name", name);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
try – catch で囲むとでません。腹たつよね。原辰徳だよね。知ってて当然みたいな顔するやつは教えるの向いてないことを自覚したほうがいいです。
実行します。
文字列で見れません。作成したJSONを見て「わーい!」ちょっとの一歩だけど大いなる一歩だ!ってやりたいのに。
return jsonObject;
ここをこうします。
return jsonObject.toString();
全体をみるとこういうメソッドにしてます。
public String jsonTest() {
JSONObject jsonObject = new JSONObject();
String name = "いやんばかん";
try {
jsonObject.put("name", name);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject.toString();
}
で、
Log.v(“AAA”, “jsonTest= ” + jsonTest());
これを実行するとぉおおお
V/AAA: jsonTest= {“name”:”いやんばかん”}
こういうのがログに出力されました、ひゃっほー。
つまり
name という入れ物に いやんばかん が入ってるという事だと理解する。
public String jsonTest() {
JSONObject jsonObject = new JSONObject();
String name = "いやんばかん";
ArrayList hako = new ArrayList();
hako.add(name);
try {
jsonObject.put("name", hako);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject.toString();
}
さて、ここから。hakoというリストを作って、そこに いやんばかん をいれる。
V/AAA: jsonTest= {“name”:”[いやんばかん]”}
こうなる。なんだよ、この [ ] は?誰に断ってつけてんだ?
2回いれてみる。
public String jsonTest() {
JSONObject jsonObject = new JSONObject();
String name = "いやんばかん";
ArrayList hako = new ArrayList();
hako.add(name);
hako.add(name);
try {
jsonObject.put("name", hako);
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject.toString();
}
こうなる。
V/AAA: jsonTest= {“name”:”[いやんばかん, いやんばかん]”}
ほほう。
[ ] はリストの事のようだな、、、。
エラー :Execution failed for task ‘:app:transformClassesWithDexForDebug’.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_73.jdk/Contents/Home/bin/java” finished with non-zero exit value 2
うーむ。なんじゃろうこれは。
調べたところ、
http://qiita.com/furu8ma/items/6cb4b08c423da6a74299
メソッドの上限があるらしく、それをオーバーしたとのこと。
確かにライブラリーに対して、ごっぽし入れ込んでしまったのが原因のようだ。