Android 오디 오 막대 그래프 효과 구현(오디 오 애니메이션 을 모방 하여 오디 오 입력 없 음)
다음 그림 에서 보 듯 이 이번 오디 오 막대 그래프 입 니 다.
 
 사용자 정의 View 의 용법 만 있 기 때문에 우 리 는 오디 오 입력 을 진실 하 게 감청 하지 않 고 무 작위 로 숫자 를 모 의 하면 된다.
위의 그림 과 같은 정적 오디 오 막대 그래프 를 실현 하려 면 여러분 들 이 생각 을 빨리 찾 을 수 있 을 것 이 라 고 믿 습 니 다.즉,하나의 사각형 을 그 리 는 것 입 니 다.모든 사각형 간 에 거 리 를 조금 만 옮 기 면 됩 니 다.다음 코드 는 좌 표를 계산 하 는 방법 을 보 여 준다.
for (int i = 0; i < mRectCount; i++) {
//              、 、  (              ,              )
canvas.drawRect(
(float) (mRectWidth * i + offset),
currentHeight,
(float) ( mRectWidth * (i + 1)),
mRectHeight,
mRectPaint
);
}
//                  ,          
mRandom = Math.random();
currentHeight = (float) (mRectHeight * mRandom);
posInvalidateDelayed(300);
@Override
protected void onSizeChanged(int w,int h,int oldW,int oldH) {
super.onSizeChanged(w, h, oldW, oldH);
//     
LinearGradient mLinearGradient;
//     
int mWidth;
//       
mWidth = getWidth();
//          
mRectHeight = getHeight();
//          (             )
mRectWidth = (mWidth-offset) / mRectCount;
//          
mLinearGradient = new LinearGradient(
0,
0,
mRectWidth,
mRectHeight,
topColor,
downColor,
Shader.TileMode.CLAMP
);
//          
mRectPaint.setShader(mLinearGradient);
}코드
다음은 이번 의 전체 코드 입 니 다.
package com.example.customaf;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import com.example.afanalog.R;
/**
*            
* Created by shize on 2016/9/5.
*/
public class MyAF extends View {
//        
private int mRectCount;
//        
private Paint mRectPaint;
//        
private int topColor, downColor;
//         
private int mRectWidth, mRectHeight;
//    
private int offset;
//     
private int mSpeed;
public MyAF(Context context) {
super(context);
}
public MyAF(Context context, AttributeSet attrs) {
super(context, attrs);
setPaint(context, attrs);
}
public MyAF(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setPaint(context, attrs);
}
public void setPaint(Context context, AttributeSet attrs){
//       TypedArray 
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MyAF);
mRectPaint = new Paint();
//            
mRectPaint.setColor(ta.getColor(R.styleable.MyAF_AFTopColor,
ContextCompat.getColor(context, R.color.top_color)));
//             
topColor=ta.getColor(R.styleable.MyAF_AFTopColor,
ContextCompat.getColor(context, R.color.top_color));
//             
downColor=ta.getColor(R.styleable.MyAF_AFDownColor,
ContextCompat.getColor(context, R.color.down_color));
//        
mRectCount=ta.getInt(R.styleable.MyAF_AFCount, 10);
//          ,       
mSpeed=ta.getInt(R.styleable.MyAF_AFSpeed, 300);
//        
offset=ta.getInt(R.styleable.MyAF_AFOffset, 5);
//   TypeArray
ta.recycle();
}
@Override
protected void onSizeChanged(int w,int h,int oldW,int oldH) {
super.onSizeChanged(w, h, oldW, oldH);
//     
LinearGradient mLinearGradient;
//     
int mWidth;
//       
mWidth = getWidth();
//          
mRectHeight = getHeight();
//          (             )
mRectWidth = (mWidth-offset) / mRectCount;
//          
mLinearGradient = new LinearGradient(
0,
0,
mRectWidth,
mRectHeight,
topColor,
downColor,
Shader.TileMode.CLAMP
);
//          
mRectPaint.setShader(mLinearGradient);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
double mRandom;
float currentHeight;
for (int i = 0; i < mRectCount; i++) {
//                  ,          
mRandom = Math.random();
currentHeight = (float) (mRectHeight * mRandom);
//              、 、  (              ,              )
canvas.drawRect(
(float) (mRectWidth * i + offset),
currentHeight,
(float) ( mRectWidth * (i + 1)),
mRectHeight,
mRectPaint
);
}
//   view    
postInvalidateDelayed(mSpeed);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.afanalog.MainActivity">
<com.example.customaf.MyAF
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:AFCount="15"
custom:AFDownColor="@color/down_color"
custom:AFSpeed="300"
custom:AFTopColor="@color/top_color"
custom:AFOffset="15"
/>
</LinearLayout>이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.