Skip to content

Instantly share code, notes, and snippets.

@YSRKEN
Last active October 27, 2015 05:09
Show Gist options
  • Save YSRKEN/1951eba554919641fe48 to your computer and use it in GitHub Desktop.
Save YSRKEN/1951eba554919641fe48 to your computer and use it in GitHub Desktop.
Android用アプリをJavaで作成してみた【スクショソフト編】 ref: http://qiita.com/YSRKEN/items/4ef1890f660a9c26c045
package com.example.(略).capture;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.StartButton);
button.setOnClickListener(this);
}
class showMessageTask extends TimerTask {
private final Handler handler = new Handler();
public void run() {
handler.post(new Runnable() {
public void run() {
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
final Date date = new Date(System.currentTimeMillis());
final File file = new File(Environment.getExternalStorageDirectory() + "/hoge/" + df.format(date) + ".png");
file.getParentFile().mkdir();
saveCapture(findViewById(android.R.id.content),file);
Toast.makeText(MainActivity.this, df.format(date), Toast.LENGTH_SHORT).show();
}
});
}
}
public void onClick(View v) {
Timer timer = new Timer();
timer.schedule(new showMessageTask(), 100, 5000);
}
// スクリーンショットを取得して保存する
public void saveCapture(View view, File file) {
Bitmap capture = getViewCapture(view);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, false);
// 画像のフォーマットと画質と出力先を指定して保存
capture.compress(CompressFormat.PNG, 100, fos);
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos == null) return;
try {
fos.close();
} catch (Exception ie) {
fos = null;
}
}
}
// スクリーンショットを取得する
public Bitmap getViewCapture(View view) {
view.setDrawingCacheEnabled(true);
Bitmap cache = view.getDrawingCache();
if(cache == null) return null;
Bitmap screen_shot = Bitmap.createBitmap(cache);
view.setDrawingCacheEnabled(false);
return screen_shot;
}
}
# PCとスマホを接続した後に下のコマンド(ポート番号は任意)を入れる
> adb tcpip 5555
# 192.168.0.17 はメモったIPアドレス。番号は先ほどと同じ
> adb connect 192.168.0.17:5555
# 接続解除するためのコマンド(PCを再起動すると自動で切られるので注意)
> adb disconnect 192.168.0.17
package com.example.(略).capture;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button; //add
import android.view.View; //add
import android.widget.Toast; //add
import android.view.View.OnClickListener; //add
//add(implements OnClickListener)
public class MainActivity extends AppCompatActivity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.StartButton); //add
button.setOnClickListener(this); //add
}
public void onClick(View v){ //add(メソッド全体)
Toast.makeText(this, "ボタンが押されました", Toast.LENGTH_LONG).show();
}
}
import java.util.Timer;
import java.util.TimerTask;
class showMessageTask extends TimerTask {
public void run() {
Toast.makeText(MainActivity.this, "メッセージ表示", Toast.LENGTH_SHORT).show();
}
}
public void onClick(View v) {
Timer timer = new Timer();
timer.schedule(new showMessageTask(), 100, 5000);
}
import android.os.Handler;
import java.util.Timer;
import java.util.TimerTask;
class showMessageTask extends TimerTask {
private final Handler handler = new Handler();
public void run() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "メッセージ表示", Toast.LENGTH_SHORT).show();
}
});
}
}
public void onClick(View v) {
Timer timer = new Timer();
timer.schedule(new showMessageTask(), 100, 5000);
}
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
import android.view.View;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public void run() {
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
final Date date = new Date(System.currentTimeMillis());
final File file = new File(Environment.getExternalStorageDirectory() + "/hoge/" + df.format(date) + ".png");
file.getParentFile().mkdir();
saveCapture(findViewById(android.R.id.content),file);
Toast.makeText(MainActivity.this, df.format(date), Toast.LENGTH_SHORT).show();
}
// スクリーンショットを取得して保存する
public void saveCapture(View view, File file) {
Bitmap capture = getViewCapture(view);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, false);
// 画像のフォーマットと画質と出力先を指定して保存
capture.compress(CompressFormat.PNG, 100, fos);
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos == null) return;
try {
fos.close();
} catch (Exception ie) {
fos = null;
}
}
}
// スクリーンショットを取得する
public Bitmap getViewCapture(View view) {
view.setDrawingCacheEnabled(true);
Bitmap cache = view.getDrawingCache();
if(cache == null) return null;
Bitmap screen_shot = Bitmap.createBitmap(cache);
view.setDrawingCacheEnabled(false);
return screen_shot;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment