gpt4 book ai didi

java - 将 MPAndroidChart 图表保存到图像而不在 Activity 中显示它

转载 作者:行者123 更新时间:2023-11-29 23:32:44 25 4
gpt4 key购买 nike

我想生成一个 PDF,但首先我需要从 LineChart(MPAndroidChart 库)创建一个图像,而不是在 Activity 中显示它。

我已遵循本教程 http://android-crap.blogspot.com/2013/02/create-bitmap-from-layoutview.html ,并且我已根据自己的要求修改了代码:

private void createLineChart(double frequency) {

LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

RelativeLayout view = new RelativeLayout(activity);
inflater.inflate(R.layout.line_chart_layout, view, true);

LineChart lc = view.findViewById(R.id.lncMain);
lc.setData(createLineData(frequency));
lc.invalidate();

view.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));

view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

Canvas c = new Canvas(b);
view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
view.draw(c);

String filename = "graph.png";
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, filename);

try {
FileOutputStream out = new FileOutputStream(dest);
b.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

Log.i(TAG, filename);
}

activity 变量是 AppCompatActivity 的实例。 R.layout.line_chart_layout的内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>

<com.github.mikephil.charting.charts.LineChart
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lncMain"
android:orientation="horizontal"
>

</com.github.mikephil.charting.charts.LineChart>

</RelativeLayout>

应用程序执行后,我得到一个名为 graph.png 的图像文件,但其内容并不反射(reflect)所需的情节:

graph.png

我已经为 view.setLayoutParamsview.layout 尝试了不同的值,但它不起作用...请提供一些建议!

最佳答案

我的猜测是 graph.png 看起来很困惑,因为 LineChart 从未真正添加到 Activity 的布局中,因此未计算其“实际大小”正确。我知道您不想向用户显示图表,但可以实现这一点,例如通过将 View 设置为不可见。

所以这是我的版本:

public class MainActivity extends AppCompatActivity {

private LineChart lc;
private RelativeLayout view;

private static final String TAG = "LCTP Main";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
protected void onResume() {
super.onResume();
if(Build.VERSION.SDK_INT > 22 && (checkSelfPermission(WRITE_EXTERNAL_STORAGE) != PERMISSION_GRANTED)) {
requestPermissions(new String[]{READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE}, 13);
}
else {
createLineChart(440.0);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 13){
boolean ok = true;
int length = permissions.length;
for (int i = 0; i < length; i++) {
String permission = permissions[i];
if(grantResults[i] != PERMISSION_GRANTED){
Log.d(TAG, "onRequestPermissionsResult: missing permission " + permission);
ok = false;
}
}
if(ok){
createLineChart(220);
}
}
}

private void createLineChart(double frequency) {
if(lc == null){
view = findViewById(R.id.layParent);
lc = findViewById(R.id.lncMain);
}
lc.setData(createLineData(frequency));

view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

Canvas c = new Canvas(b);
view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
view.draw(c);

String filename = "graph.png";
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, filename);

try {
FileOutputStream out = new FileOutputStream(dest);
b.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

Log.i(TAG, filename);
}
});
lc.invalidate();
}

private LineData createLineData(double frequency) {
List<Entry> yVals = new ArrayList<>();
for(int i = 0; i < frequency; i = i + 10){
yVals.add(new Entry(i, (float)Math.sin(i*i)));
}
ILineDataSet lineDataSet = new LineDataSet(yVals, "LabelString");
LineData lineData = new LineData(lineDataSet);
return lineData;
}
}

activity_main.xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Hello World!"/>

<include
layout="@layout/line_chart_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"/>

</LinearLayout>

生成的 png:

enter image description here

关于java - 将 MPAndroidChart 图表保存到图像而不在 Activity 中显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52504666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com