gpt4 book ai didi

android - 保留 fragment

转载 作者:行者123 更新时间:2023-11-29 15:09:33 29 4
gpt4 key购买 nike

我正在编写一个可以在 Canvas 上绘制气泡的应用程序。我有 MainActivity,它的布局是一个简单的 LinearLayout,我将其用作 fragment 的容器。当我在 Canvas 上绘图时,我的 fragment 没有 xml 布局,所以我像这样以编程方式设置它的布局:

public class BubbleFragment extends Fragment {

Bubble bubble;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

//retain fragment
setRetainInstance(true);

//bubble = new Bubble(getActivity()); //THIS WILL CRASH APP, MOVE TO onCreateView instetad
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
100);
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);

// instantiate my class that does drawing on Canvas
bubble = new Bubble(getActivity());
bubble.setLayoutParams(lp);
bubble.setBackgroundColor(Color.BLUE);
ll.addView(bubble); //if you create bubble in onCreate() this will crash. Create bubble in onCreateView

return ll;
}
}

因此,当我启动我的应用程序时,我希望气泡显示在屏幕中间并慢慢向底部移动。因为我在上面使用了 setRetainInstance(true),所以我希望当我旋转屏幕时,气泡会在旋转前停止的地方继续。但是,它会在其初始位置(屏幕中间)重绘。

我想继续从屏幕方向改变之前的位置开始绘制自己,而不是从头开始。

这是我的气泡代码:

public class Bubble extends View {

private static final boolean BUBBLING = true; //thread is running to draw

private Paint paint;
private ShapeDrawable bubble;

// coordiantes, radius etc
private int x;
private int y;
private int dx;
private int dy;
private int r;
private int w = 400;
private int h = 400;

//handler to invalidate (force redraw on main GUI thread from this thread)
private Handler handler = new Handler();

public Bubble(Context context) {
super(context);

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
w = size.x;
h = size.y;

x = w/2;
y = h/2;
r = 60;
dx = 1;
dy = 1;

bubble = new ShapeDrawable(new OvalShape());
bubble.getPaint().setColor(Color.RED);
bubble.setBounds(0, 0, r, r);

paint = new Paint();
paint.setAntiAlias(true);
paint.setStrokeWidth(10);
}

@Override
protected void onSizeChanged (int w, int h, int oldw, int oldh){
//set bubble parameters (center, size, etc)

startAnimation();
}

public void startAnimation(){
new Thread(new Runnable() {
public void run() {
while (BUBBLING) {
moveBubble();

try {
Thread.sleep(200);
} catch (InterruptedException e) {

}

//update by invalidating on main UI thread
handler.post(new Runnable() {
public void run() {
invalidate();
}
});
}
}
}).start();
}


@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);

canvas.save();
// draws bubble on canvas
canvas.translate(dx, dy);
bubble.draw(canvas);
canvas.restore();
}

private void moveBubble(){
dx += 1;
dy += 1;
x = x + dx;
y = y + dy;
if (bubble.getPaint().getColor() == Color.YELLOW){
bubble.getPaint().setColor(Color.RED);
} else {
bubble.getPaint().setColor(Color.YELLOW);
}
}
}

非常感谢,

最佳答案

如果你真的想保留整个 View ,你可以做一些像延迟加载这样的事情:

public class BubbleFragment extends Fragment {
Bubble bubble;
LinearLayout parent;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setRetainInstance(true);
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
parent = new LinearLayout(activity);
if(bubble == null) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
100);
bubble = new Bubble(getActivity());
bubble.setLayoutParams(lp);
bubble.setBackgroundColor(Color.BLUE);
}
parent.setOrientation(LinearLayout.VERTICAL);
parent.addView(bubble);
}

@Override
public void onDetach() {
super.onDetach();
parent.removeView(bubble);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return parent;
}
}

关于android - 保留 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33159777/

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