我想從我的android應用程序打開相機時顯示一條線來回移動屏幕。 (就像在條碼掃描器應用程序中移動的那條線一樣)。 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. 掃描條形碼和顯示自動
- 2. 在另一個活動中顯示無線掃描的結果
- 3. Scandit條碼掃描器在掃描時顯示空白相機
- 4. OCR掃描不顯示掃描輸出
- 5. 如何在android中顯示條形碼掃描器
- 6. CRT顯示器中的光柵掃描
- 7. 如何在cameraView中顯示掃描動畫
- 8. 斑馬線庫不顯示在片段活動掃描結果
- 9. 如何在顯示活動前顯示進度條
- 10. 掃描自己的線程中的條形碼(ZXing)並顯示一個ProgessDialog
- 11. 在條碼掃描器中顯示邊界區域
- 12. 在OpenGL中繪製閃亮的燈光以模擬舊顯示器掃描線
- 13. 如何顯示進度條?
- 14. 如何從QR碼掃描中將圖像(離線)顯示爲動作?
- 15. 如何在BlackBerry中顯示進度條?
- 16. 顯示正在通過glob()掃描的文件夾php
- 17. 如何顯示移動的圓圈以顯示在網站上運行的進程
- 18. 移動JFrame以正確顯示JCombobox
- 19. 顯示進度條動畫
- 20. 在運行時在BlackBerry設備上掃描顯示的文本
- 21. 實施進度條顯示正在進行的工作
- 22. 如何獲取移動Safari中顯示的滾動條?
- 23. 如何在文本框內顯示qr掃描儀掃描結果
- 24. Captuvo iPod的條形碼掃描儀顯示睡眠
- 25. 正在顯示九條貼片線
- 26. 如何在android中顯示動作欄上的進度條?
- 27. 如何在正在運行的活動中顯示活動?
- 28. 如何在UITableView中顯示滾動條
- 29. 如何在NestedScrollView中顯示滾動條
- 30. 如何在win32中使用線程功能顯示進度條?
我該如何將此添加到主要活動? – neo
查看我的編輯:將2個默認構造函數添加到類中,將視圖放入您的activity_main.xml中,並以常規視圖從onCreate中獲取 – NSimon
位置根本不會改變。 –