私はアンドロイドアプリケーションからカメラを開いている間、画面を前後に移動する線を表示したいと思います。 (バーコードスキャナアプリケーションで動く線のように)。このためLike the white line shown in the image. The line can move from top to bottom and bottom to top continuously進行中のスキャンが進行中であることを表示するにはどうすればいいですか?
0
A
答えて
1
、あなたはつもりだカスタムビューを作成し、以下のようonDrawメソッドをオーバーライドしたい:
public class MyScanningView extends View {
private Paint paint = new Paint();
private int mPosY = 0;
private boolean runAnimation = true;
private boolean showLine = true;
private Handler handler;
private Runnable refreshRunnable;
private boolean isGoingDown = true;
private int mHeight;
public MyScanningView(Context context) {
super(context);
init();
}
public MyScanningView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyScanningView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint.setColor(Color.WHITE);
//Add anything else you want to customize your line, like the stroke width
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
refreshView();
}
}
}
@Override
public void onDraw(Canvas canvas) {
mHeight = canvas.getHeight();
if (showLine) {
canvas.drawLine(0, mPosY, canvas.getWidth(), mPosY, paint);
}
if (runAnimation) {
handler.postDelayed(refreshRunnable, DELAY);
}
}
public void startAnimation() {
runAnimation = true;
showLine = true;
this.invalidate();
}
public void stopAnimation() {
runAnimation = false;
showLine = false;
reset();
this.invalidate();
}
private void reset() {
mPosY = 0;
isGoingDown = true;
}
private void refreshView() {
//Update new position of the line
if (isGoingDown) {
mPosY += 5;
if (mPosY > mHeight) {
//We invert the direction of the animation
mPosY = mHeight;
isGoingDown = false;
} else {
mPosY -= 5;
if (mPosY < 0) {
//We invert the direction of the animation
mPosY = 0;
isGoingDown = true;
}
this.invalidate();
}
}
そして、単にあなたのactivy_main.xmlにこのビューを追加します。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.//wherever_is_your_view.MyScanningView
android:id="@+id/scanningView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
そして、あなたのMainActivityで:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyScanningView scanningView = (MyScanningView) findViewById(R.id.scanningView);
}
0
以下のコードを使用して、xmlでそのビューを作成し、drawlineメソッドのスキャンの制限を制限します。 ScanningIndicatorのobject.startAnimation()を使用してアニメーションを開始します。
public class ScanningIndicator extends View {
private Paint paint = new Paint();
private int mPosY = 0;
private boolean runAnimation = true;
private boolean showLine = true;
private Handler handler;
private Runnable refreshRunnable;
private boolean isGoingDown = true;
private int mHeight;
public ScanningIndicator(Context context) {
super(context);
init();
}
public ScanningIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ScanningIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint.setColor(Color.RED);
paint.setStrokeWidth(5.0f);
//Add anything else you want to customize your line, like the stroke width
handler = new Handler();
refreshRunnable = new Runnable() {
@Override
public void run() {
refreshView();
}
};
}
@Override
public void onDraw(Canvas canvas) {
mHeight = canvas.getHeight();
if (showLine) {
canvas.drawLine(0, mPosY, canvas.getWidth(), mPosY, paint);
}
if (runAnimation) {
handler.postDelayed(refreshRunnable, 0);
}
}
public void startAnimation() {
runAnimation = true;
showLine = true;
this.invalidate();
}
public void stopAnimation() {
runAnimation = false;
showLine = false;
reset();
this.invalidate();
}
private void reset() {
mPosY = 0;
isGoingDown = true;
}
private void refreshView() {
//Update new position of the line
if (isGoingDown) {
mPosY += 5;
if (mPosY > mHeight) {
mPosY = mHeight;
isGoingDown = false;
}
} else {
//We invert the direction of the animation
mPosY -= 5;
if (mPosY < 0) {
mPosY = 0;
isGoingDown = true;
}
}
this.invalidate();
}
}
関連する問題
- 1. React-Native:ファイルのダウンロード中に進行状況を表示するにはどうすればいいですか?
- 2. 進行中のタスクをタイムアウトメカニズムで中断するにはどうすればいいですか?
- 3. フラグメントに中間進行バーを表示するにはどうすればよいですか?
- 4. プロセスが進行中であるかどうかをチェック
- 5. 進行中のGWT RequestBuilderリクエストを中止するにはどうすればよいですか?
- 6. 進行中のトランザクションなしで外部コールを行うにはどうすればよいですか?
- 7. メソッドがWindowsアプリケーションで実行中に進行状況を表示
- 8. 進行状況バーの進行状況を変更するにはどうすればいいですか?
- 9. Android:プログレスバーで進行中の通知を作成するにはどうすればよいですか?
- 10. javascriptで進行中のプロセスを作成するにはどうすればよいですか?
- 11. Snap.svg:進行中のドラッグ操作をプログラムでキャンセルするにはどうすればよいですか?
- 12. ビルダー通知で進行中のフラグを設定するにはどうすればよいですか?
- 13. Visual Studio TestMethodの実行中に進行状況を表示することはできますか?
- 14. 角度コンパイルの進行中にアニメーションを表示する方法
- 15. 進行中のオーディオタスクを継続するにはどうすればよいですか?
- 16. 進行中のSCNTransactionを終了するにはどうすればよいですか?
- 17. ダウンロード中にPHP/Curlが進行する
- 18. Nodejs - 別のインストールが進行中です
- 19. UIViewController:識別オリエンテーションが進行中です
- 20. 進行スピナーを持つダイアログを表示するにはどうすればいいですか?
- 21. Androidの進行状況バーにダウンロードの進行状況を表示するにはどうすればいいですか?
- 22. uisliderなしでストリームの進行状況を表示するにはどうすればよいですか?
- 23. winformクライアントでSQL Server SPの進行状況を表示するにはどうすればよいですか?
- 24. CKEditorでアップロードの進行状況を表示するにはどうすればよいですか?
- 25. は、ネットワークコール中に進行状況バーを表示します。
- 26. 進行中の進行中のプロセスの例PlayFramework 2
- 27. 同期中の進行中のバーのファイル名を表示
- 28. 進行状況バーにアップロードの割合を表示するにはどうすればよいですか?
- 29. プログラムの実行中に完了の進行状況を表示
- 30. 進捗ダイアログで進行中のバーが表示されますが、メッセージが更新されません
これをメインアクティビティに追加するにはどうすればよいですか? – neo
私の編集を参照してください:クラスに2つのデフォルトのコンストラクタを追加し、activity_main.xmlにビューを入れ、onCreateから定期ビューとして取り出します – NSimon
位置はまったく変更されません。 –